[mod_python] Mod_python & cPickle / saving Instances

Graham Dumpleton graham.dumpleton at gmail.com
Tue Apr 1 05:57:06 EDT 2008


Read:

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

Graham

On 01/04/2008, Dominique.Holzwarth at ch.delarue.com
<Dominique.Holzwarth at ch.delarue.com> wrote:
> Hey Guys
>
>  I was just playing around with the cPickle module to save some instance objects into binary files and 'offline' (normal python interpreter on local machine) it all works fine. However, when I try to do the same things under apache with mod_python I get a mod_python specific error...
>
>  Here are my testing files:
>
>  Pickletest.py
>  *******************************************************************************************************************
>  from mod_python import apache
>  from mod_python import Session
>  import cPickle, pickle
>
>  myModule = apache.import_module('~/test/dummyclass.py')
>
>  secretText = "hallo"
>
>  def index(req):
>     req.session = Session.FileSession(req, secret=secretText)
>     if req.session.is_new():
>         myInstance = myModule.myClass()
>         myInstance.hit()
>         hits = str(myInstance.hitCounter)
>         myOtherInstance = myModule.myOtherClass()
>         myOtherInstance.hit()
>         randNum = str(myOtherInstance.randomNumber)
>         file = open('C:\\Program Files\\Apache Software Foundation\\Apache2.2\\test\\myInstances.pkl','wb')
>         cPickle.dump((myInstance, myOtherInstance), file)
>         file.close()
>         req.session.save()
>         return "Hits: "+hits+" Random Number: "+randNum
>     else:
>         file = open('C:\\Program Files\\Apache Software Foundation\\Apache2.2\\test\\myInstances.pkl','rb')
>         (myInstance, myOtherInstance) = cPickle.load(file)
>         file.close()
>         myInstance.hit()
>         myOtherInstance.hit()
>         hits = str(myInstance.hitCounter)
>         randNumber = str(myOtherInstance.randomNumber)
>         req.session['myInstances'] = (myInstance, myOtherInstance)
>         file = open('C:\\Program Files\\Apache Software Foundation\\Apache2.2\\test\\myInstances.pkl','wb')
>         cPickle.dump((myInstance, myOtherInstance), file)
>         file.close()
>         req.session.save()
>         return "Hits: "+hits+" Random Number: "+randNum+" old session"
>  *******************************************************************************************************************
>
>  This script does nothing more than saving / loading instance objects and displaying some sort of 'hit counter'
>
>  Dummyclass.py
>  *******************************************************************************************************************
>  import random
>
>  class myClass:
>     def __init__(self):
>         self.attribute = 1
>         self.text = "hello world"
>         self.hitCounter = 0
>
>     def hit(self):
>         self.hitCounter += 1
>
>  class myOtherClass:
>     def __init__(self):
>         self.otherAttribute = 2
>         self.otherText = "goodbye world"
>         self.randomNumber = 0
>
>     def hit(self):
>         self.randomNumber = random.randint(0, 1000)
>  *******************************************************************************************************************
>  Defines some useless classes, of which instance objects should be stored :-)
>
>  The script manages to write the *.pkl file which looks like this:
>
>  myInstances.pkl
>  *******************************************************************************************************************
>  ((i_mp_bee88a136571a696398a0eb82b06eeb3
>  myClass
>  p1
>  (dp2
>  S'attribute'
>  p3
>  I1
>  sS'hitCounter'
>  p4
>  I1
>  sS'text'
>  p5
>  S'hello world'
>  p6
>  sb(i_mp_bee88a136571a696398a0eb82b06eeb3
>  myOtherClass
>  p7
>  (dp8
>  S'otherText'
>  p9
>  S'goodbye world'
>  p10
>  sS'otherAttribute'
>  p11
>  I2
>  sS'randomNumber'
>  p12
>  I105
>  sbt.
>  *******************************************************************************************************************
>
>  As I understand the pickle-algorithm this file looks as intended except for the i_mp_bee88a136571a696398a0eb82b06eeb3 which normaly would be the name of the module. Obviously the module name is converted into some mod_python GUID thingy...
>
>  The error message i get is the following:
>
>  MOD_PYTHON ERROR
>
>  ProcessId:      3784
>  Interpreter:    'xx'
>
>  ServerName:     'xx'
>  DocumentRoot:   'C:/Program Files/Apache Software Foundation/Apache2.2/htdocs'
>
>  URI:            '/python/test/pickletest.py'
>  Location:       None
>  Directory:      'C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/python/'
>  Filename:       'C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\python\\test\\pickletest.py'
>  PathInfo:       ''
>
>  Phase:          'PythonHandler'
>  Handler:        'mod_python.publisher'
>
>  Traceback (most recent call last):
>
>   File "C:\Program Files\Python25\lib\site-packages\mod_python\importer.py", line 1537, in HandlerDispatch
>     default=default_handler, arg=req, silent=hlist.silent)
>
>   File "C:\Program Files\Python25\lib\site-packages\mod_python\importer.py", line 1229, in _process_target
>     result = _execute_target(config, req, object, arg)
>
>   File "C:\Program Files\Python25\lib\site-packages\mod_python\importer.py", line 1128, in _execute_target
>     result = object(arg)
>
>   File "C:\Program Files\Python25\lib\site-packages\mod_python\publisher.py", line 213, in handler
>     published = publish_object(req, object)
>
>   File "C:\Program Files\Python25\lib\site-packages\mod_python\publisher.py", line 425, in publish_object
>     return publish_object(req,util.apply_fs_data(object, req.form, req=req))
>
>   File "C:\Program Files\Python25\lib\site-packages\mod_python\util.py", line 554, in apply_fs_data
>     return object(**args)
>
>   File "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\python\test\pickletest.py", line 25, in index
>     (myInstance, myOtherInstance) = cPickle.load(file)
>
>  ImportError: No module named _mp_bee88a136571a696398a0eb82b06eeb3
>
>
>  MODULE CACHE DETAILS
>
>  Accessed:       Tue Apr 01 11:05:33 2008
>  Generation:     26
>
>  _mp_bee88a136571a696398a0eb82b06eeb3 {
>   FileName:     'C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\python\\test\\dummyclass.py'
>   Instance:     1
>   Generation:   13
>   Modified:     Tue Apr 01 09:59:54 2008
>   Imported:     Tue Apr 01 10:05:22 2008
>  }
>
>  _mp_754c5030ce462c9ea7741cb6f66d6cc1 {
>   FileName:     'C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\python\\test\\pickletest.py'
>   Instance:     13
>   Generation:   26
>   Modified:     Tue Apr 01 10:56:58 2008
>   Imported:     Tue Apr 01 10:57:10 2008
>   Children:     'C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\python\\test\\dummyclass.py'
>  }
>
>  Is there a way how to use cPickle AND mod_python together? I also tried to use the session mechanism instead of the pickle mechanism to store instance objects in a binary file but that doesn't seem to work either (if I do a req.session['myInstances'] = (myInstance, myOtherInstance) then the session doesn't seem to be saved properly and thus, everytime I hit on browser-reload a new session is generated...)
>
>  Would be really great if someone could help me with this!
>
>  Greetings Dominique
>
>
>  _______________________________________________
>  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