Sessions for Tornado (patch included)

1,102 views
Skip to first unread message

Milan Cermak

unread,
Jan 25, 2010, 7:55:41 PM1/25/10
to Tornado Web Server
Hi,

I've implemented session support for Tornado. You can find the commit
here http://github.com/milancermak/tornado/commit/5cf388672cdd492c3f76fd96aad5b3526ece0789
(I'm new to Git and GitHub, I hope everyone can access it).

The commit provides file and MySQL based sessions. Every request
handler has a session object, which behaves as a python dictionary;
plus, it has a couple utility functions available. For additional
info, check out the in-code docs.

It is in no way complete. The biggest issue in my opinion is, that the
developer has to explicitly save the session if the data changes. I
believe this could be resolved quite easily by adding a couple of
lines to the code.

Please let me know what you think, any critique is welcomed.

Milan

Matt Ferguson

unread,
Jan 26, 2010, 3:53:39 AM1/26/10
to python-...@googlegroups.com
This is nice, especially for those of us who believe that solutions
such as Beaker are a little too heavy to justify. I will fork this
and try to improve it a little, and add support for more backend
solutions (memcached, redis, etc as it's my personal belief that MySQL
is a little heavy for use with sessions)

Sent from my iPhone

On Jan 25, 2010, at 4:55 PM, Milan Cermak <milan....@gmail.com>
wrote:

wataka

unread,
Jan 26, 2010, 4:10:45 AM1/26/10
to Tornado Web Server
Nice. If you have a stub, I might try getting the MongoDb backend done

On Jan 26, 9:53 am, Matt Ferguson <mbf...@gmail.com> wrote:
> This is nice, especially for those of us who believe that solutions  
> such as Beaker are a little too heavy to justify.  I will fork this  
> and try to improve it a little, and add support for more backend  
> solutions (memcached, redis, etc as it's my personal belief that MySQL  
> is a little heavy for use with sessions)
>
> Sent from my iPhone
>

> On Jan 25, 2010, at 4:55 PM, Milan Cermak <milan.cer...@gmail.com>  


> wrote:
>
> > Hi,
>
> > I've implemented session support for Tornado. You can find the commit

> > herehttp://github.com/milancermak/tornado/commit/5cf388672cdd492c3f76fd96...

Milan Cermak

unread,
Jan 26, 2010, 6:50:37 AM1/26/10
to Tornado Web Server

On Jan 26, 10:10 am, wataka <nhy...@googlemail.com> wrote:
> Nice. If you have a stub, I might try getting the MongoDb backend done
>
> On Jan 26, 9:53 am, Matt Ferguson <mbf...@gmail.com> wrote:
>
> > This is nice, especially for those of us who believe that solutions  
> > such as Beaker are a little too heavy to justify.  I will fork this  
> > and try to improve it a little, and add support for more backend  
> > solutions (memcached, redis, etc as it's my personal belief that MySQL  
> > is a little heavy for use with sessions)
>

Cool. I have similar believes; my plan is to provide at least
Memcached storage. Redis and Mongo would be great. Plus, I was
persuaded that a file based storage, where every session is stored in
a separate file is also a valid approach so I want to support it too.

I will commit couple of small improvements later this evening, mostly
regarding the MySQLSession class.

Sylvain

unread,
Jan 26, 2010, 9:30:32 AM1/26/10
to Tornado Web Server
This project gmemsess : http://code.google.com/p/gmemsess/
is a secure lightweight memcache-backed session class for Google
appengine

So if you want session + tornado framework and GAE, it's very easy to
convert from webapp to tornado.

Regards.

Joe Bowman

unread,
Jan 26, 2010, 9:50:18 AM1/26/10
to Tornado Web Server
One thing to consider about using memcache only on appengine is that
the memcache there is volatile. It's shared across all applications so
you can run into instances where your memcache area can be dropped in
favor of a more active application.

http://gaeutilities.appspot.com/ provides a session class that uses
memcache and the backend datastore together. It also supports cookie
only sessions, and includes a rotating token system to provide more
security for sessions, especially in instances where you can't use ssl
(ssl is only available for .appspot.com domains, not your own).
Gaeutilities also has several other utility classes, such as a cache,
and a Retry and Timeout model to provide more stability for your
application on top of the appengine datastore. It also is written in
such a way as you should easily be able to plug it in to tornado no
problem.

from appengine_utilities import session

my_session = session.Session()
my_session[key] = value

The one caveat is that I've moved away from appengine for development
of my primary application, so while I do still provide patches for
issues when they are brought up, I'm also looking for someone
qualified to take over the project.

Matthew Ferguson

unread,
Jan 26, 2010, 4:13:35 AM1/26/10
to python-...@googlegroups.com
MongoDB is my database of choice, it'd be very simple to plug that into Tornado for sessions code.

Joe Bowman

unread,
Jan 27, 2010, 9:08:14 AM1/27/10
to Tornado Web Server
For my current app, which is not appengine based, I wrote a pure
memcached backed sessions library. It's not fully tested, but has
worked for my purposes of development so far. It also has the token
rotation that gaeutilities has. If anyone wants to use it for any
reason, you can get it at the link below. It's 100% public domain, I'm
not offering any support or anything for it, however, I'll try to keep
it up to date if I make changes.

http://github.com/joerussbowman/tornado-mc-sessions

Milan Cermak

unread,
Feb 15, 2010, 6:59:21 AM2/15/10
to Tornado Web Server
I've now added support for MongoDB, Redis, MySQL and Memcached
sessions to my fork. For those interested in a simple benchmark of
these session engines, visit
http://milancermak.posterous.com/benchmarking-tornados-sessions-0

On Jan 26, 10:13 am, Matthew Ferguson <mbf...@gmail.com> wrote:
> MongoDB is my database of choice, it'd be very simple to plug that into Tornado for sessions code.
>
> On Jan 26, 2010, at 1:10 AM, wataka wrote:
>
> > Nice. If you have a stub, I might try getting the MongoDb backend done
>
> > On Jan 26, 9:53 am, Matt Ferguson <mbf...@gmail.com> wrote:
> >> This is nice, especially for those of us who believe that solutions  
> >> such as Beaker are a little too heavy to justify.  I will fork this  
> >> and try to improve it a little, and add support for more backend  
> >> solutions (memcached, redis, etc as it's my personal belief that MySQL  
> >> is a little heavy for use with sessions)
>
> >> Sent from my iPhone
>
> >> On Jan 25, 2010, at 4:55 PM, Milan Cermak <milan.cer...@gmail.com>  
> >> wrote:
>
> >>> Hi,
>

> >>> I've implementedsessionsupport for Tornado. You can find the commit


> >>> herehttp://github.com/milancermak/tornado/commit/5cf388672cdd492c3f76fd96...
> >>> (I'm new to Git and GitHub, I hope everyone can access it).
>
> >>> The commit provides file and MySQL based sessions. Every request

> >>> handler has asessionobject, which behaves as a python dictionary;


> >>> plus, it has a couple utility functions available. For additional
> >>> info, check out the in-code docs.
>
> >>> It is in no way complete. The biggest issue in my opinion is, that the

> >>> developer has to explicitly save thesessionif the data changes. I

Reply all
Reply to author
Forward
0 new messages