Saturday, March 3, 2012

Gevent+Psycopg2 = execute cannot be used while an asynchronous query is underway

I am using a stack composed of, among others, Django + Gevent + Gunicorn + psycopg2 (greened with https://bitbucket.org/dvarrazzo/psycogreen/src).
Until recently, I was occasionally getting the following errors, if I rapidly reloaded our application that generates a couple of ajax calls on the startup:
ProgrammingError: execute cannot be used while an asynchronous query is underway
DatabaseError: execute cannot be used while an asynchronous query is underway
After getting the following versions:
Django 1.4 b1
Gunicorn 0.14
Gevent 0.13.6
psycopg2 2.4.4
And greening psycopg2 pre_fork like this (in the Gunicorn config):
worker_class = "gevent"
def def_pre_fork(server, worker):
    from psyco_gevent import make_psycopg_green
    make_psycopg_green()
    worker.log.info("Made Psycopg Green")
pre_fork = def_pre_fork
And using the gunicorn_django in a virtualenv to run the server from supervisor (instead of the manage.py shortcut):
[program:photobooks]
command=/path/bin/gunicorn_django -c /path/conf/photobooks_gunicorn.conf.py
directory=/path/src/photobooks
environment=PATH="/path/bin"
I was not able to reproduce the problem any more.

2 comments:

  1. Could this be related to threading? The runserver command in Django 1.4 (maybe past releases as well, I'm not sure) has a --nothreading command. Did you try the runserver with this?

    Also curious if you're using werkzeug and if that causes any unintended effects when debugging a "green" server.

    ReplyDelete