[mod_python] Sessions (Aaaarggghhhh!!)

Graham Dumpleton grahamd at dscpl.com.au
Tue Jan 17 06:00:26 EST 2006


On 17/01/2006, at 5:07 PM, Joseph Barillari wrote:

> On Tue, Jan 17, 2006 at 12:33:59AM -0500, Graham Dumpleton wrote:
>> Luis M. Gonzalez wrote ..
>>> Thank you Jim!!
>>>
>>>  You were right.
>>>  Instead of saving the cart items into a cart instance, I did it  
>>> into a
>>> simple dictionary.
>>>  I also saved the dict only once at the end the code block.
>>
>> To provide some background as to why saving class objects is not  
>> good,
>> have a read of:
>>
>>   http://www.dscpl.com.au/articles/modpython-005.html
>
> Interesting. Is there any way to store a code object in a mod_python
> session? From what I've read, that general technique lets you do some
> neat tricks:
>
> http://lib.store.yahoo.net/lib/paulgraham/bbnexcerpts.txt

Rather than storing in a session an actual function object, you can
store the full path of the module and the name of the function. Ie.,
something like:

   def callback():
     ...

   def handler(req):
     ...
     session["callback"] = (__file__,"callback")
     session.save()

Later on when the session is restored and you need to call the function
you can do something like:

     (file,function_name) = session["callback"]
     (directory,module_name) = os.path.split(file)
     module_name = os.path.splitext(module_name)[0]

     module = apache.import_module(module_name,path=[directory])
     getattr(module,function_name)()

In other words you just use a level of indirection and do the lookup of
the module and function yourself, rather than pickle doing it for you.

The restoration will hopefully be simpler in mod_python 3.3 if I my
proposal for changes to apache.import_module() is accepted. There you
will hopefully be able to also supply the full path to the module as
first argument instead. Ie.,

     (file,name) = session["callback"]
     module = apache.import_module(file)
     getattr(module,name)()

Does this allow you to achieve what you want?

PS, I haven't actually read the article you referenced yet as getting
late at night and brain not able to comprehend it.

Graham



More information about the Mod_python mailing list