Trouble storing object in session variable (ActivePython/IIS)

Robert Brewer fumanchu at amor.org
Mon Nov 10 12:54:09 CET 2003


Robin Edgar Hansen wrote:
> I'd like to store an object in a session variable and thus keep it
> 'alive' for the entire session...BUT, it seems only simple variables
> can be stored as session variables.

Did you get an answer to this yet? My typical means of dealing with this
problem is to rely on the persistence of loaded modules; that is, the
fact that each module you reference in your application is probably only
going to be loaded once. I must admit I haven't done much per-Session
work, but it works well for Application data, both in IIS and
mod_python/Apache. For Session data, you might create a dictionary (of
the complex data you wish to store) in a module, and simply save the
keys in the Session object. Run a thread to garbage-collect the dead
entries after a period of inactivity.

So somewhere in telnetlib:

clients = {}

def _sequencer():
    _XUnit_sequence = sys.maxint
    while 1:
        yield _XUnit_sequence
        _XUnit_sequence -= 1
        if _XUnit_sequence <= 0:
            _XUnit_sequence = sys.maxint
_uniqueid = _sequencer()

def new_client():
    cli = Telnet("localhost",9999)
    id = _uniqueid.next()
    clients[id] = cli
    return id


Then in session start code:
import telnetlib
Session.SetValue("cli", telnetlib.new_client())


Then later in the session:
cli = telnetlib.clients[Session.Value("cli")]


That's just one way, and not specifically tested (the *concept* works
;). Maybe it'll give you some ideas.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org





More information about the Python-list mailing list