zl程序教程

您现在的位置是:首页 >  Python

当前栏目

python连接池interfaceerror

2023-04-18 14:42:23 时间

Unfortunately this is a problem with django + psycopg2 + celery combo.

It's an old and unsolved problem.

Take a look on this thread to understand:

https://github.com/celery/django-celery/issues/121

Basically, when celery starts a worker, it forks a database connection

from django.db framework. If this connection drops for some reason, it

doesn't create a new one. Celery has nothing to do with this problem

once there is no way to detect when the database connection is dropped

using django.db libraries. Django doesn't notifies when it happens,

because it just start a connection and it receives a wsgi call (no

connection pool). I had the same problem on a huge production

environment with a lot of machine workers, and sometimes, these

machines lost connectivity with postgres server.

I solved it putting each celery master process under a linux

supervisord handler and a watcher and implemented a decorator that

handles the psycopg2.InterfaceError, and when it happens this function

dispatches a syscall to force supervisor restart gracefully with

SIGINT the celery process.