[mod_python] Session Hanging Problems

David Janes davidjanes at blogmatrix.com
Tue Nov 27 14:06:03 EST 2007


Here's what I'm experimenting with right now -- I'm subclassing
FileSession and overriding all the lock methods. Obviously this will
have to be adapted for people not working in the FileSession world.

Regards, etc...

    class BMFileSession(Session.FileSession):
        def __init__(self, req, sid = None):
            self.bm_lockf = None
            ... more init stuff ...


        def lock(self):
            if self._lock:
                self.bm_lock(self._req.server, self._sid)
                self._locked = 1
                self._req.register_cleanup(Session.unlock_session_cleanup, self)

        def unlock(self):
            if self._lock and self._locked:
                self.bm_unlock(self._req.server, self._sid)
                self._locked = 0

        def lock_file(self):
            if not self._locked:
                self.bm_lock(self._req.server, self._sid)
                self._locked = 1

        def unlock_file(self):
            if self._locked and not self._lock:
                self.bm_unlock(self._req.server, self._sid)
                self._locked = 0

        def bm_lockfile(self, sid):
            lockdir = os.path.join(self._sessdir, sid[:2])
            if not os.path.exists(lockdir):
                os.makedirs(lockdir)

            return  os.path.join(lockdir, sid)

        def bm_lock(self, server, sid):
            if self.bm_lockf:
                try: self.bm_lockf.close()
                except: pass

            self.bm_lockf = open(self.bm_lockfile(sid), 'a+')
            fcntl.lockf(self.bm_lockf, fcntl.LOCK_EX)

        def bm_unlock(self, server, sid):
            if self.bm_lockf:
                fcntl.lockf(self.bm_lockf, fcntl.LOCK_UN)

                try: self.bm_lockf.close()
                except: pass

                self.bm_lockf = None


More information about the Mod_python mailing list