[mod_python] How isolate class attributes across requests?

Graham Dumpleton grahamd at dscpl.com.au
Fri Aug 18 04:37:58 EDT 2006

On 18/08/2006, at 8:07 PM, Norman Tindall wrote:

> Hello,
>     As i understand mod_python one interpreter per site
>     default policy, if i import somemodule
>     and then change attribute of class (
>     for example from
>             @classmethod
>                   def someclass_method(cls,arg):
>                   cls.atrib = arg
>     or with
>              setattr(class,'atrib_name',value) ).
>     Then in all other requests to this site i will have this attribute
>     of class changed (as they will run within same interpreter).

Correct. Do be aware that this would have to be done at time of
module import otherwise multiple requests in distinct threads
could fight to do it at the same time if there is not adequate thread

>     Is there any methods to isolate class attributes across requests
>     EXCEPT running interpreter per request, defining class within
>     handler, writing code without class attributes?
>     I mean elegant and high-speed solution.

You might be able to use thread local storage if your version of
Python supports it, or simply resort to a data structure which is
indexed by the thread identifier, ie., threading.currentThread().

Am not sure that would really be what you want though as
subsequent requests can reuse the same thread. Another
alternative is to use the value of 'req.connection.id'. This is a
unique ID which Apache allocates on a per process basis (I
think) to identify individual connections handled by that process.

In either case, you may need to register a request cleanup handler
to delete any temporary data which was created related to the request.

>     I bear in mind only one way out: to create instance of one (first)
>     class, store desired attributes as an instance attributes,
>     then spawn instances of other (second) class and they will have
>     needed attrubutes not as class attributes but as instance of first
>     class attributes.
>     But i dont like it, isolating class attributes across request
>     would be more elegant.

Not quite sure what you mean here, but the most common way of
having per request data is simply to store it as an attribute in the
request object itself as most of the time that will be passed around
to handlers fulfilling the request anyway. Thus:

   class RequestData:

   req.my_request_data = RequestData()

Because the request object gets destroyed at the end of the request
your request specific data will also be destroyed.

>     By the way what is the avarage speed-drop down when using
>     interpreter per request in compare with interpreter per
>     directory/site?

You can't have a distinct interpreter for each distinct request  
unless you
use prefork MPM (UNIX only) and set maximum requests per child to 1.
This would be very slow and only recommended during development
for debugging.

Maybe you can provide a better more complete example of what you
want to do.


More information about the Mod_python mailing list