persistant web state

Bengt Richter bokr at oz.net
Tue May 20 16:30:33 EDT 2003


On Tue, 20 May 2003 09:10:29 -0700, "Jay O'Connor" <joconnor at nets.com> wrote:

>All,
>
>I'm lloking for a technique to allow me to keep memory alive between
>invocations of a cgi program.    What I mean is that if I have two cgi
>requests come into a script, they should be able to access the same data
>in memory and not have to load from a database.
>
>Some context:  years ago I wrote a web server front end for Smalltalk
>applications.   What this allows is that the server runs continously
>between web requests and, as a result, it can cache data as Smalltalk
>objects in it's own memory and not have to reload data from disk or db
>between web requests.   This makes life easier for the developer as well
>as providing good performance.
>
>I'd like to carry that model over into Python, and I can think of a
>couple of techniques:
>
>Use Python CGIs, but have them get their objects from a running python
>object server using either sockets or possibly stdin/stdout streams.  The
>python object server would be permanatly running and would cache database
>hit results as Python objects.
>
>Use one of the Python HTTPServers and just have python handle all web
>serving, keeping the objects in between requests in the server memory.
>
>Others?  Suggestions?
>
I haven't tried this, but IWT you could share pickled objects
in a memory mapped file. You'd need a lock mechanism for updates,
and you'd need some kind of allocation and directory mechanism.
Perhaps a fixed overwritable space at the front could be reserved for
a size-prefixed pickled directory of names whose associated values
are th binary-file positions of corresponding pickles, so you could
do something like

(warning! untested! need exception protection for locks, etc. etc.)

def getSharedThing(sharingName, mfile):
    <get read share lock> # whatever the mechanism
    mfile.seek(0)
    mdict = cPickle.load(mfile)
    mfile.seek(mdict[sharingName])
    retval = return cPickle.load(mfile)
    <release read share lock>
    return retval

You could prefix the file with an update counter, so you could
cache mdict and skip loading it if there was no update between calls
to getSharedThing. It would make sense to make this a method of a class
that handles everything inited with a file path and some access/role parameter.

I would think this could work in a CGI Python script.
Of course, there is a security problem if the shared
file can be modified by untrusted entities, so the CGI
should be running as a user with carefully set privs
and file access rights, IWT.

Anyway, just a thought OTTOMH ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list