:::: MENU ::::

Posts Categorized / web2py

  • Mar 14 / 2014
  • 0
Python, web2py

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('created_datetime', 'datetime', default=now),
Field('modified_datetime', 'datetime'),
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:


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():
    start = time.time()
    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).

  • Jan 27 / 2014
  • 0
Python, web2py

Web2py framework

Do you like web development and Python ? You’re looking for a framework free, complete and in a constant evolution (features, security, …) ? So Web2py is for you !

web2py is a free, open-source web framework for agile development of secure database-driven web applications; it is written in Python and programmable in Python. web2py is a full-stack framework, meaning that it contains all the components you need to build fully functional web applications.

web2py is designed to guide a web developer to follow good software engineering practices, such as using the Model View Controller (MVC) pattern. web2py separates the data representation (the model) from the data presentation (the view) and also from the application logic and workflow (the controller). web2py provides libraries to help the developer design, implement, and test each of these three parts separately, and makes them work together.

web2py is built for security. This means that it automatically addresses many of the issues that can lead to security vulnerabilities, by following well established practices. For example, it validates all input (to prevent injections), escapes all output (to prevent cross-site scripting), renames uploaded files (to prevent directory traversal attacks). web2py takes care of main security issues, so developers have less chances of introducing vulnerabilities.

web2py includes a Database Abstraction Layer (DAL) that writes SQL dynamically so that you, the developer, don’t have to. The DAL knows how to generate SQL transparently for SQLite, MySQL, PostgreSQL, MSSQL, FireBird, Oracle, IBM DB2, Informix and Ingres.

The DAL can also generate function calls for the Google Datastore when running on the Google App Engine (GAE). Experimentally we support more databases and new ones are constantly added. Please check on the web2py web site and mailing list for more recent adapters. Once one or more database tables are defined, web2py automatically generates a fully functional web-based database administration interface to access the database and the tables.

web2py differs from other web frameworks in that it is the only framework to fully embrace the Web 2.0 paradigm, where the web is the computer. In fact, web2py does not require installation or configuration; it runs on any architecture that can run Python (Windows, Windows CE, Mac OS X, iOS, and Unix/Linux), and the development, deployment, and maintenance phases for the applications can be done via a local or remote web interface. web2py runs with CPython (the C implementation) and PyPy (Python written in Python), on Python versions 2.5, 2.6, and 2.7.

web2py provides a ticketing system for error events. If an error occurs, a ticket is issued to the user, and the error is logged for the administrator.

web2py is open source and released under the LGPL version 3 license.

Official website : Official Website Web2py
Informations source : Web2py Book

Question ? Contact