librelist archives

« back to archive

How to do server side cleanup on session expiration?

How to do server side cleanup on session expiration?

From:
Sanjay Bhandari
Date:
2013-02-02 @ 05:42
 have some objects allocated and maintained server side for the duration of
a session. How can I make sure that these are cleaned up after a session
expires?

Is there support in Flask (or some Flask extension) for this?

Re: [flask] How to do server side cleanup on session expiration?

From:
Ron DuPlain
Date:
2013-02-04 @ 18:55
On Sat, Feb 2, 2013 at 12:42 AM, Sanjay Bhandari wrote:
> have some objects allocated and maintained server side for the duration of a
> session. How can I make sure that these are cleaned up after a session
> expires?
>
> Is there support in Flask (or some Flask extension) for this?

Flask sessions by default are stored in a signed cookie, i.e. entirely
browser-side. Flask supports implementation of a server-side session
using the SessionInterface API.

http://flask.pocoo.org/docs/api/#session-interface

You can find snippets on how to do this here:

http://flask.pocoo.org/snippets/category/sessions/

If you need to do clean up work asynchronously (timed instead of
triggered necessarily), a task queue might be what you want.

http://celeryproject.org/
http://python-rq.org/

Or you can setup a cron job with something like Flask-Script.

-Ron

Re: [flask] How to do server side cleanup on session expiration?

From:
Sanjay Bhandari
Date:
2013-02-04 @ 19:18
> If you need to do clean up work asynchronously (timed instead of
triggered necessarily), a task queue might be what you want.

You mention triggered. That is the part I am trying to understand, really.
Is there a way that Flask can "trigger" a cleanup based on its notion of
when the session has expired? Is there such a mechanism / facility? Some
callback, or event etc.

I can't seem to find it in SessionInterface (or anywhere else).


On Mon, Feb 4, 2013 at 1:55 PM, Ron DuPlain <ron.duplain@gmail.com> wrote:

> On Sat, Feb 2, 2013 at 12:42 AM, Sanjay Bhandari wrote:
> > have some objects allocated and maintained server side for the duration
> of a
> > session. How can I make sure that these are cleaned up after a session
> > expires?
> >
> > Is there support in Flask (or some Flask extension) for this?
>
> Flask sessions by default are stored in a signed cookie, i.e. entirely
> browser-side. Flask supports implementation of a server-side session
> using the SessionInterface API.
>
> http://flask.pocoo.org/docs/api/#session-interface
>
> You can find snippets on how to do this here:
>
> http://flask.pocoo.org/snippets/category/sessions/
>
> If you need to do clean up work asynchronously (timed instead of
> triggered necessarily), a task queue might be what you want.
>
> http://celeryproject.org/
> http://python-rq.org/
>
> Or you can setup a cron job with something like Flask-Script.
>
> -Ron
>

Re: [flask] How to do server side cleanup on session expiration?

From:
Ron DuPlain
Date:
2013-02-04 @ 19:30
On Mon, Feb 4, 2013 at 2:18 PM, Sanjay Bhandari <sanjay@ziffusion.com> wrote:
>> If you need to do clean up work asynchronously (timed instead of
> triggered necessarily), a task queue might be what you want.
>
> You mention triggered. That is the part I am trying to understand, really.
> Is there a way that Flask can "trigger" a cleanup based on its notion of
> when the session has expired? Is there such a mechanism / facility? Some
> callback, or event etc.
>
> I can't seem to find it in SessionInterface (or anywhere else).

Not in the built-in session, because it is cookie-based. You have no
means to get a callback when the cookie expires, since the session
only exists in the browser and in transit in the HTTP headers. As
such, any interactions with the session itself cannot guarantee when
to trigger a clean up, since the browser may not ever make a follow-up
request.

The solutions you are left with are:

1) Use a server-side session implementation which uses
SessionInterface. It's up to that session implementation to provide
some sort of triggering of sessions or inspection of recently expired
sessions.

2) Schedule a clean up task that looks for things to do. Clean things
up 24h, 2d, 1w, or whatever time makes sense to your application. You
can do this in cron or with a task queue.

This is without knowing anything about your application, though. You
mention allocating objects. If you mean objects in memory within your
Python process, then you will run into some serious limitations.
First, you should clean up immediately when the request is done, or
face memory leaks. Second, sharing across Python processes becomes
problematic.

Hope this helps,

Ron