views:

31

answers:

1

I'm using CherryPy to make a web-based frontend for SymPy that uses an asynchronous process library on the server side to allow for processing multiple requests at once without waiting for each one to complete. So as to allow for the frontend to function as expected, I am using one process for the entirety of each session. The client-side Javascript sends the session-id from the cookie to the server when the user submits a request, and the server-side currently uses a pair of lists, storing instances of a controller class in one and the corresponding session-id's in another, creating a new interpreter proxy and sending the input if a non-existant session-id is submitted. The only problem with this is that the proxy classes are not deleted upon the expiration of their corresponding sessions. Also, I can't see anything to retrieve the session-id for which the current request is being served.

My questions about all this are: is there any way to "connect" an arbitrary object to a CherryPy session so that it gets deleted upon session expiration, is there something I am overlooking here that would greatly simplify things, and does CherryPy's multi-threading negate the problem of synchronous reading of the stdout filehandle from the child process?

+1  A: 

You can create your own session type, derived from CherryPy's base session. Use its clean_up method to do your cleanup.

Look at cherrypy/lib/sessions.py for details and sample session implementations.

Ivo van der Wijk
Thank you very much! Two questions, though: how do I get CherryPy to use the new session object, and I notice that there is no `__del__` method defined on the base Session class, so do I need to check for expired sessions regularly, and if so, how do I do that?
Bushman
Getting CherryPy to use it is pretty ugly; you stick a reference to your custom session class in `cheryrpy.lib.sessions`, and then use the same name (without the 'Session' suffix) in config. For example, if you made a MyCustomSession class, write "from cherrypy.lib import sessions" then "class MyCustomSession(sessions.FileSession): ..." then "sessions.MyCustomSession = MyCustomSession" then, in config: "tools.sessions.storage_type = 'mycustom'"
fumanchu