[mod_python] Hello all, Questions about Mod Python sessions on Linux

michael bayer mike_mp at zzzcomputing.com
Sat Nov 13 14:53:44 EST 2004


yikes !   a database connection is an active network connection to a 
database server in the current application's process space.  if you 
pack it up into a file, which is what the session really is, and pull 
it out again, its meaningless.

I would make a bet that your windows installation is working better 
than the linux one becuase the windows session is probably storing 
everything in memory.  but then, if you are holding one database 
connection per user in each user's session, what if you have 50,000 
users all visit your site in a day ?  youd be storing 50,000 database 
connections.  actually you wouldnt since your app would crash long 
before you got there.

just store user data only in the session...a good rule of thumb for 
starters is, if its not a string, dont store it.  don't put any kind of 
application resources in there.

On Nov 12, 2004, at 6:54 PM, Trevor West wrote:

> Hi Michael,
>
> The really funny thing is that it should be an instance, and it is the 
> user specific information instance that I want to keep in the session. 
> Something is really strange between my linux and windows build though. 
> I can do very little with sessions on Linux for some reason. I've 
> pulled apart my instance so that I'm now only trying to save the 
> database connection (again something that works on Windows) and I get 
> this error instead:
>
> Mod_python error: "PythonHandler mod_python.publisher"
>
> Traceback (most recent call last):
>
>  File "/usr/local/lib/python2.3/site-packages/mod_python/apache.py", 
> line 299, in HandlerDispatch
>    result = object(req)
>
>  File 
> "/usr/local/lib/python2.3/site-packages/mod_python/publisher.py", line 
> 136, in handler
>    result = util.apply_fs_data(object, req.form, req=req)
>
>  File "/usr/local/lib/python2.3/site-packages/mod_python/util.py", 
> line 361, in apply_fs_data
>    return object(**args)
>
>  File "/usr/local/apache2/htdocs/test/mptest.py", line 43, in login
>    sess.save()
>
>  File "/usr/local/lib/python2.3/site-packages/mod_python/Session.py", 
> line 204, in save
>    self.do_save(dict)
>
>  File "/usr/local/lib/python2.3/site-packages/mod_python/Session.py", 
> line 329, in do_save
>    dbm[self._sid] = cPickle.dumps(dict)
>
>  File "/usr/local/lib/python2.3/copy_reg.py", line 70, in _reduce_ex
>    state = base(self)
>
> TypeError: connect() argument 1 must be string, not Connection
>
> I'm wondering if my installation should be done again, or if I'm not 
> doing the configuration of apache correctly, because as far as I can 
> tell I'm writing the code correctly, and I'm running it on another OS 
> and it works fine (I pulled it apart like you suggested so that all 
> I'm saving now is the database connection, the username and a couple 
> other small pieces of data).
>
> Cheers
>
> Trevor West
>
>
> michael bayer wrote:
>
>> seems like you are trying to store a Python module in the Session, 
>> and Pickle isnt able to do that.
>>
>> regardless of which platforms this may or may not work on, it 
>> generally doesn't make sense to store a module in the session, since 
>> a Python module and its instance variables, assuming normal usage via 
>> "import", are all shared amongst many requests and possibly across 
>> multiple concurrent threads, whereas the Session stores information 
>> specific to one user and that user's subset of requests.
>>
>> you probably want to figure out what application state it is exactly 
>> you want to store on a per-session basis, and stick it into an object 
>> instance that you can safely pickle away.
>>
>> if you are unsure what object in your Session is a module, you can 
>> always try walking through all the elements in the session and test 
>> obj.__class__.__name__ == 'module' to see who the offending element 
>> is.
>>
>> On Nov 10, 2004, at 4:24 PM, Trevor West wrote:
>>
>>> Setups for use in the below discussion:
>>> Machine 1:
>>> Windows 2000
>>> Apache 2.0.52
>>> Mod Python 3.1.3
>>> MySQL_Python
>>> Python 2.3
>>> wxPython
>>>
>>>
>>> Machine 2:
>>> Fedora Core 2
>>> Apache 2.0.52
>>> Mod Python 3.1.3
>>> MySQL_Python
>>> Python 2.3
>>> wxPython
>>>
>>> I'm writing a thin-client to a wxPython application and when I run 
>>> the thin client on Machine 1(the windows box) everything works 
>>> great, no problem. While when I try to move it to what is now the 
>>> production box, Machine 2 (linux box), I get this error:
>>>
>>> Mod_python error: "PythonHandler mod_python.publisher"
>>>
>>> Traceback (most recent call last):
>>>
>>>  File "/usr/local/lib/python2.3/site-packages/mod_python/apache.py", 
>>> line 299, in HandlerDispatch
>>>    result = object(req)
>>>
>>>  File 
>>> "/usr/local/lib/python2.3/site-packages/mod_python/publisher.py", 
>>> line 136, in handler
>>>    result = util.apply_fs_data(object, req.form, req=req)
>>>
>>>  File "/usr/local/lib/python2.3/site-packages/mod_python/util.py", 
>>> line 361, in apply_fs_data
>>>    return object(**args)
>>>
>>>  File "/usr/local/apache2/htdocs/test/mptest.py", line 42, in login
>>>    sess.save()
>>>
>>>  File 
>>> "/usr/local/lib/python2.3/site-packages/mod_python/Session.py", line 
>>> 204, in save
>>>    self.do_save(dict)
>>>
>>>  File 
>>> "/usr/local/lib/python2.3/site-packages/mod_python/Session.py", line 
>>> 329, in do_save
>>>    dbm[self._sid] = cPickle.dumps(dict)
>>>
>>>  File "/usr/local/lib/python2.3/copy_reg.py", line 69, in _reduce_ex
>>>    raise TypeError, "can't pickle %s objects" % base.__name__
>>>
>>> TypeError: can't pickle module objects
>>>
>>> So, as far as I can tell, on my Windows box I'm able to add class 
>>> instances to the session (in this case, I'm sure it's my database 
>>> class that is causing this) but on Linux, it won't add it to the 
>>> session.
>>>
>>> If anyone can give him a heads up that would be great. I've been 
>>> beating my head against the wall with the linux box because the 
>>> windows setup was SO easy, and being a big linux fan I was 
>>> disappointed in the strange problems I've run into.
>>>
>>> Anyway, thanks in advance.
>>>
>>> Trevor West
>>> ------------------------
>>> Developer for QA Labs
>>>
>>> _______________________________________________
>>> Mod_python mailing list
>>> Mod_python at modpython.org
>>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
>> _______________________________________________
>> Mod_python mailing list
>> Mod_python at modpython.org
>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>


More information about the Mod_python mailing list