[mod_python] Sessions performance and some numbers

dharana dharana at dharana.net
Thu Apr 7 11:01:34 EDT 2005



Graham Dumpleton wrote:
> 
> On 07/04/2005, at 8:08 PM, dharana wrote:
> 
>> If you want I can send my modified Session.py with the new FileSession 
>> class for review.
> 
> 
> There probably shouldn't have been a need for you to copy/modify the actual
> Session.py file which came with mod_python as your derived version could
> live quite happily in its own module and simply used the installed Session
> module.
> 

I presumptuously thought that it could fit into the official mod_python 
package due to it's high performance when compared to DbmSession.

 >
> Anyway, by all means post your code as sure it will be of interest to
> someone, if not now then maybe in the future. If there are any problems
> in what you have done, someone is also bound to point it out.
> 

Here it goes. Please point out any obvious problem. Apart from being new 
to mod_python I'm also new to Python in general. For example, I don't 
think the exception handling I've put is completely correct.


In anticipation for any possible attachment problems i pasted it 
directly. (I have read PEP 0008 and the 4 spaces indentation level 
recommendation but I'm in a hurry right now, sorry.)


--- FileSession.py -----------------------------------------------------
import cPickle
import tempfile

from mod_python import Session

tempdir = tempfile.gettempdir()


class FileSession(Session.BaseSession):

   def __init__(self, req, sid=0, secret=None, timeout=0, lock=1):

     Session.BaseSession.__init__(self, req, sid=sid, secret=secret,
                          timeout=timeout, lock=lock)

   def do_cleanup(self):
     import os

     # is there any faster way of doing this?
     for f in os.listdir(tempdir):
       if f.find('mp_sess_', 0, 11) == -1:
         continue

       fp = file('%s%s' % (tempdir, f))
       dict = cPickle.load(fp)
       fp.close()

       if (time() - dict['_accessed']) > dict['_timeout']:
         os.unlink('%s%s' % (tempdir, f))


   def do_load(self):
     try:
     # again, is there a more pythonic way of doing this check?
       fp = file('%s/mp_sess_%s' % (tempdir, self._sid))
     except Exception:
       return None
     else:
       try:
         data = cPickle.load(fp)
         fp.close()
         return data

       except Exception:
         fp.close()
         pass


   def do_save(self, dict):
     fp = file('%s/mp_sess_%s' % (tempdir, self._sid), 'w+')
     cPickle.dump(dict, fp)
     fp.close()


   def do_delete(self):
     try:
       unlink('%s/mp_sess_%s' % (tempdir, self._sid))
     except Exception:
       pass
------------------------------------------------------------------------

-- 
Juan Alonso
http://gamersmafia.com | http://laflecha.net
-------------- next part --------------
import cPickle
import tempfile

from mod_python import Session

tempdir = tempfile.gettempdir()


class FileSession(Session.BaseSession):

  def __init__(self, req, sid=0, secret=None, timeout=0, lock=1):

    Session.BaseSession.__init__(self, req, sid=sid, secret=secret,
                         timeout=timeout, lock=lock)

  def do_cleanup(self):
    import os

    # is there any faster way of doing this?
    for f in os.listdir(tempdir):
      if f.find('mp_sess_', 0, 11) == -1:
        continue

      fp = file('%s%s' % (tempdir, f))
      dict = cPickle.load(fp)
      fp.close()

      if (time() - dict['_accessed']) > dict['_timeout']:
        os.unlink('%s%s' % (tempdir, f))


  def do_load(self):
    try:
    # again, is there a more pythonic way of doing this check?
      fp = file('%s/mp_sess_%s' % (tempdir, self._sid))
    except Exception:
      return None
    else:
      try:
        data = cPickle.load(fp)
        fp.close()
        return data

      except Exception:
        fp.close()
        pass


  def do_save(self, dict):
    fp = file('%s/mp_sess_%s' % (tempdir, self._sid), 'w+')
    cPickle.dump(dict, fp)
    fp.close()


  def do_delete(self):
    try:
      unlink('%s/mp_sess_%s' % (tempdir, self._sid))
    except Exception: 
      pass


More information about the Mod_python mailing list