django-celery with postgres: “connection already closed” errors

I ran into a mysterious, transient exception this week using django-celery.

DatabaseError: server closed the connection unexpectedly
This probably means the server terminated abnormally
    before or while processing the request.

At other times, I was seeing a slightly different error message.

InterfaceError: connection already closed

After some searching, I found resolved issue #149 in the django-celery maintainer's github issues's list. It turns out that it's specifically a postgres issue, and only happens when the number of workers (the "c" parameter) is greater than one. Note: it defaults to the number of cores on your system.

A new issue #46 has been opened, so this will likely get fixed. In the meantime, you can monkey patch the code to fix it yourself.

sudo vim /usr/local/lib/python2.6/dist-packages/djcelery/

    # inside DjangoLoader
    def on_task_init(self, task_id, task):