PylonsHQ.

Layout: Fixed-width

Issues with long-running Pylons processes

When your Pylons application remains running for a long time (as opposed to one-off request handling) some unique issues often arise. This page tries to list known issues and provide solution or a workaround.

Timed-out database connections (MySQL)

MySQL typically has an idle timout for connections (on Ubuntu it is set by default to 8 hours). This means if no-one uses your app for 8 hours, open database connection silently 'expires' and next attempt to do a database query will result in: OperationalError: (2013, 'Lost connection to MySQL server during query').

MySQLdb python module doesn't provide the code to deal with this (e.g. to re-connect automatically) so this becomes your responsibility. Popular ORMs (such as SQLObject and SQLAlchemy) provides some workarounds though.

If you are using SQLAlchemy you should use a connection pool and specify the pool_recycle option to be less than the connection timeout so that SQLAlchemy re-creates the connection. See http://www.sqlalchemy.org/docs/dbengine.myt for more information.

SQLObject 0.8 attempts to re-execute query up to three times if SERVER_LOST or SERVER_GONE errors are reported by MySQLdb.

If all above fails, you can setup a cron task which will ping your server regularly and prevent db connection to expire, e.g. wget http://localhost:5009/.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Powered by Pylons - Contact Administrators