[Stackless] http session management

Aaron Watters aaron at reportlab.com
Fri Sep 6 19:06:51 CEST 2002

One of the biggest pains of doing work with http is
trying to keep track of what's going on in a complex
multistage transaction, when http itself is "stateless".

This generally results in lots of hidden variables and
the usual state-machine code obfuscation.

Just wanted to share that I'm having some success
keeping track of "sessions" using tasklets that get "reconnected"
to http interactions within an http server.  My first test
for managing sessions within the framework looks like this
at present..

def testprocessor(session, channel, scheduler):
    donesignal = None
        # generate fibonnaci numbers
        a = b = 1
        memo = "guest"
        for i in range(6):
            # finish last interaction
            if donesignal is not None:
            # wait for next connection for this session
            (dictionary, socket, Sock, addr, donesignal, debug) = 
            scheduler.send(socket, "HTTP/1.0 200 OK\r\n")
            scheduler.send(socket, "Content-type: text/html\r\n")
            scheduler.send(socket, "\r\n")
            scheduler.send(socket, """
            Welcome %s to session %s<hr>
            i=%s a=%s b=%s<br>
            <a href="test.cgi?session=%s">go around again!</a>
            """ % (memo, session, i, a, b, session,))
            a, b = b, a+b
            memo = "back"
        scheduler.send(socket, "<h3>This session is done now</h3>")
        if donesignal is not None and donesignal.balance<0: # ???

In the session tasklet this function generates 6 screens in sequence 
when the user presses
"go around again".  The first looks like this:

Welcome guest to session 1031330478.68
i=0 a=1 b=1
go around again! <http://localhost:8000/test.cgi?session=1031330368.23>

and the last looks like this

Welcome back to session 1031330478.68
i=5 a=8 b=13
go around again! <http://localhost:8000/test.cgi?session=1031330478.68>

      This session is done now

The interesting thing is that this is "straight line code" even though
the 6 http interactions are independent.

Having implemented more  HTTP state machines than a sane person should
be permitted on one lifetime I'm finding this alternative quite 
refreshing :).

I'll share  some code once I've played a bit more.

   -- Aaron Watters

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.stackless.com/pipermail/stackless/attachments/20020906/049486db/attachment.htm>

More information about the Stackless mailing list