List Info

Thread: class attributes and mod_python




class attributes and mod_python
country flaguser name
United States
2007-05-25 06:52:55
I am working on a cherrypy application with a simple form.
The form is
submitted to a method submit_job together with all form
parameteres. I
don't want to send all parameters in the form request, so i
thought i
could save them as class attributes like this:
class Page( object) :
  def __init__ (self):
    self.var1 = random.randint()
  def index(self):
    self.var2 = "test"
  def submit_job(self, var1):
    print var1
    print self.var1
    print self.var2

This works when i am running the build-in cherrypy server. I
also
submit var1 with the form for some validation. The variables
var1 en
self.var1 are now the same and self.var2 is available.

When i try this on my production server which is running
Apache and
mod_python, this trick stops working. The variables var1 is
not the
same anymore as self.var1 (indicating that a new instances
of the Page
object has been made?) and also self.var2 does not exist.
How can this
be explained?

What is the best way to use class attributes within the
submit_job
method without passing them into the form? Should i use
sessions? Or
are there any other solutions within cherrypy? Or could it
be a Apache
thing?

Any help is appreciated...


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: class attributes and mod_python
country flaguser name
United States
2007-05-25 11:55:58
mdehollandergmail.com wrote:
> I am working on a cherrypy application with a simple
form. The form is
> submitted to a method submit_job together with all form
parameteres. I
> don't want to send all parameters in the form request,
so i thought i
> could save them as class attributes like this:
> class Page( object) :
>   def __init__ (self):
>     self.var1 = random.randint()
>   def index(self):
>     self.var2 = "test"
>   def submit_job(self, var1):
>     print var1
>     print self.var1
>     print self.var2
>   

Those aren't class attributes.  They are instance
attributes.  However,
that's irrelevant here.

A mod_python-based server runs in multiple threads, and
there's no
guarantee that the same object -- or even the same instance
of the
interpreter -- will handle the request every time.  You
can't rely on
instance attributes persisting from one request to another.

> What is the best way to use class attributes within the
submit_job
> method without passing them into the form? Should i use
sessions? Or
> are there any other solutions within cherrypy? Or could
it be a Apache
> thing?
>   

You either need to pass everything in the form or use a
session.

-- 
Tim Roberts, timrprobo.com
Providenza & Boekelheide, Inc.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: class attributes and mod_python
country flaguser name
United States
2007-05-25 13:48:13
Thanks for your reply and the information on mod_python. I
will try if
i can get it working with sessions.

On May 25, 6:55 pm, Tim Roberts <t...probo.com> wrote:
> mdehollan...gmail.com wrote:
> > I am working on a cherrypy application with a
simple form. The form is
> > submitted to a method submit_job together with all
form parameteres. I
> > don't want to send all parameters in the form
request, so i thought i
> > could save them as class attributes like this:
> > class Page( object) :
> >   def __init__ (self):
> >     self.var1 = random.randint()
> >   def index(self):
> >     self.var2 = "test"
> >   def submit_job(self, var1):
> >     print var1
> >     print self.var1
> >     print self.var2
>
> Those aren't class attributes.  They are instance
attributes.  However,
> that's irrelevant here.
Yes, you're right. Shame on me.

>
> A mod_python-based server runs in multiple threads, and
there's no
> guarantee that the same object -- or even the same
instance of the
> interpreter -- will handle the request every time.  You
can't rely on
> instance attributes persisting from one request to
another.
Ah, that explains a lot.

>
> > What is the best way to use class attributes
within the submit_job
> > method without passing them into the form? Should
i use sessions? Or
> > are there any other solutions within cherrypy? Or
could it be a Apache
> > thing?
>
> You either need to pass everything in the form or use a
session.
Ok, i will try the sessions first.

>
> --
> Tim Roberts, t...probo.com
> Providenza & Boekelheide, Inc.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: class attributes and mod_python
country flaguser name
United States
2007-05-25 18:14:19
On May 26, 2:55 am, Tim Roberts <t...probo.com> wrote:
> mdehollan...gmail.com wrote:
> > I am working on a cherrypy application with a
simple form. The form is
> > submitted to a method submit_job together with all
form parameteres. I
> > don't want to send all parameters in the form
request, so i thought i
> > could save them as class attributes like this:
> > class Page( object) :
> >   def __init__ (self):
> >     self.var1 = random.randint()
> >   def index(self):
> >     self.var2 = "test"
> >   def submit_job(self, var1):
> >     print var1
> >     print self.var1
> >     print self.var2
>
> Those aren't class attributes.  They are instance
attributes.  However,
> that's irrelevant here.
>
> Amod_python-based server runs in multiple threads, and
there's no
> guarantee that the same object -- or even the same
instance of the
> interpreter -- will handle the request every time.  You
can't rely on
> instance attributes persisting from one request to
another.

Not quite. Is it not the multiple threads that are the
problem as all
threads in a process run in the same interpreter for the
application.
It is a common misconception that each request thread runs
in a
different interpreter instance. It is simply not the case.

The real problem is that Apache uses multiple processes for
handling
requests. Thus subsequent requests may not even be handled
in the same
process.

For further information read:

  http://www.dscpl.com.au/wiki/ModPython
/Articles/TheProcessInterpreterModel

Graham


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "cherrypy-users" group.
To post to this group, send email to cherrypy-usersgooglegroups.com
To unsubscribe from this group, send email to
cherrypy-users-unsubscribegooglegroups.com
For more options, visit this group at h
ttp://groups.google.com/group/cherrypy-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )