[mod_python] Problems inheriting BaseSession

R. Pelizzi r.pelizzi at virgilio.it
Fri Aug 27 21:09:40 EDT 2004


A few days ago i was trying to implement a "counter" for a site... i had 
to keep track of the number of active sessions and i was asking for a 
way to store variables global to the whole server. There was no way to 
do that, so i tought i could implement a dbm db file and pickle the 
values. I can't see any way to implement an efficient locking mechanism 
(since semaphores cannot be shared between interpreters), what would you 
use? Would using files as semaphores be very inefficient?
Anyway, i thought i would just implement locking later, and i started 
the implementation. I created a class called Application, which is 
perfectly working (tough not thread-safe as i explained before) and can 
be access like a dictionary.
Ok, then i needed a way to register a cleanup function. To use 
Session.cleanup i had to subclass BaseSession. This is where the 
problems come in.

Look at the code:
------------------
from mod_python import Session
import anydbm, UserDict, pickle

class Application:
     """Questa classe permette di condividere tra più istanze oggetti 
serializzabili
     """
     def __init__(self):
         self.file = anydbm.open("boh", "c")
         self.load()

     def __len__(self):
         return len(self.dict)

     def __getitem__(self, key):
         return self.dict[key]

     def __setitem__(self, key, value):
         self.dict[key] = value

     def save(self):
         self.file['dict'] = pickle.dumps(self.dict)

     def load(self):
         try:
             self.dict = pickle.loads(self.file['dict'])
         except KeyError:
             #il file è nuovo
             self.dict = {}

     def __del__(self):
         self.save()

class MySession(Session.BaseSession):
     def __init__(self, req):
         Session.BaseSession.__init__(self, req)
         Session.CLEANUP_CHANCE = 5
         self.dict = Application()
         try:
             self.dict["count"] += 1
         except KeyError:
             self.dict["count"] = 1

     def cleanup():
         self.dict["count"] -= 1

     def countActiveSessions():
         return self.dict["count"]

def index(req):
     sess = MySession(req)
     return "ciao"
--------------------

now, this is the error i get when i run application.py:
-------------------
Mod_python error: "PythonHandler mod_python.publisher"

Traceback (most recent call last):

   File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 
299, in HandlerDispatch
     result = object(req)

   File "/usr/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/lib/python2.3/site-packages/mod_python/util.py", line 361, 
in apply_fs_data
     return object(**args)

   File "/home/riccardo/web/application.py", line 50, in index

   File "/home/riccardo/web/application.py", line 35, in __init__
     Session.Session.__init__(req)

   File "/usr/lib/python2.3/site-packages/mod_python/Session.py", line 
124, in __init__
     if self.load():

   File "/usr/lib/python2.3/site-packages/mod_python/Session.py", line 
185, in load
     dict = self.do_load()

AttributeError: 'MySession' object has no attribute 'do_load'
-------------------------

what is do_load? and how come MySession hasn't got a method called 
do_load if it is a child of BaseSession?

Have i made any mistakes that prevent MySession to be a 100% child of 
BaseSession? This would be a very strange behaviour... Any clues?

thank you


More information about the Mod_python mailing list