Unserialize web2py requests and allow concurrent access

With a default configuration for your web2py application, you will notice that when you are launching an action, if this one is quite long to execute, you won’t be able to open a new tab to run another request on the same application. This can be explained because when you are launching an action, your browser is sending session informations to the server and your session file on server-side is so opened in writing mode for being updated. A lock is set on this file, blocking any other action to be executed for this session while the lock is not released.

This problem is effectively not existing if you are using a database for your session management instead of a file system. To use a database system, you just need to declare your database:

db = DAL(...)

and initiate the connection to this database for sessions:

session.connect(request, response, db)

A table will so be automatically created, named web2py_session_appname and will contain following fields:

Field('locked', 'boolean', default=False),
Field('client_ip'),
Field('created_datetime', 'datetime', default=now),
Field('modified_datetime', 'datetime'),
Field('unique_key'),
Field('session_data', 'text')

If you want to use the file system for your sessions, you will need to use the following command to allow the session file release:

session.forget()

You will notice that this command must only be called if the action you want to execute do not require to session informations. For example, if you are loading dynamically some contents via Ajax calls on several parts of the page, you can add this command for each call to allow the concurrential execution knowing that the connection informations are saved while main page is loading.

As testing, you can use following code:

import time
def session_lock():
    session.forget(response)
    start = time.time()
    time.sleep(3)
    stop = time.time() - start
    return "Generated on %s - Took %s sec" % (request.now, int(stop))

def session_lockall():
    a = LOAD('test', 'session_lock', ajax=True)
    b = LOAD('test', 'session_lock', ajax=True)
    c = LOAD('test', 'session_lock', ajax=True)
    d = LOAD('test', 'session_lock', ajax=True)
    e = LOAD('test', 'session_lock', ajax=True)
    f = LOAD('test', 'session_lock', ajax=True)
    g = LOAD('test', 'session_lock', ajax=True)
    h = LOAD('test', 'session_lock', ajax=True)
    return locals()

If you’re calling previous code by deleting the session.forget() command call, you will get this as a result:

a : Generated on 2014-03-14 20:35:25.830665 - Took 3 sec
b : Generated on 2014-03-14 20:35:28.446957 - Took 3 sec
c : Generated on 2014-03-14 20:35:31.433133 - Took 3 sec
d : Generated on 2014-03-14 20:35:34.435814 - Took 3 sec
e : Generated on 2014-03-14 20:35:37.438998 - Took 3 sec
f : Generated on 2014-03-14 20:35:40.442821 - Took 3 sec
g : Generated on 2014-03-14 20:35:43.446179 - Took 3 sec
h : Generated on 2014-03-14 20:35:46.872932 - Took 3 sec

If you’re calling the same code with the session.forget() command call, you will get:

a : Generated on 2014-03-14 20:41:57.796238 - Took 3 sec
b : Generated on 2014-03-14 20:41:57.798177 - Took 3 sec
c : Generated on 2014-03-14 20:41:57.841212 - Took 3 sec
d : Generated on 2014-03-14 20:41:57.835651 - Took 3 sec
e : Generated on 2014-03-14 20:41:57.839484 - Took 3 sec
f : Generated on 2014-03-14 20:41:57.837849 - Took 3 sec
g : Generated on 2014-03-14 20:42:00.850737 - Took 3 sec
h : Generated on 2014-03-14 20:42:00.878716 - Took 3 sec

We can notice that between both cases, we have a concurrential execution thanks to the session.forget() function, and that we can hardly optimize the page loading time.

Please note that most of browsers won’t allow you to execute more than 6 requests at the same time, hence the result we are getting with the last test where we can see that the six first requests are executed together whereas the following ones are executed afterwards (on a second flow but concurrent also).


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *