项目背景:
- Django项目,我使用gunicorn运行项目,在项目中,我使用python socket io处理一些事件
- Postgresql,配置如下:
DATABASES['default'] = {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': xxx,
'USER': xxx,
'PASSWORD': xxx,
'HOST': 'xxx',
'PORT': '5432',
'CONN_MAX_AGE': 60 * 10, # seconds
'OPTIONS': {
'connect_timeout': 20,
},
}
python socket io 将保留一个线程来处理一些事件。所以线程有自己的postgresql数据库连接。有时候,我发现它会抛出数据库连接问题,像这样,我不知道为什么数据库连接没有正常关闭
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/engineio/server.py", line 520, in _trigger_event
return self.handlers[event](*args)
File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 590, in _handle_eio_message
self._handle_event(sid, pkt.namespace, pkt.id, pkt.data)
File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 526, in _handle_event
self._handle_event_internal(self, sid, data, namespace, id)
File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 529, in _handle_event_internal
r = server._trigger_event(data[0], namespace, sid, *data[1:])
File "/usr/local/lib/python2.7/site-packages/socketio/server.py", line 558, in _trigger_event
return self.handlers[namespace][event](*args)
File "/usr/src/app/async_worker/controllers/server/event_handler.py", line 61, in on_jira_handle_from_client_retrieve
ticket = Ticket.get_ticket_by_id(ticket_id)
File "/usr/src/app/review/models/ticket.py", line 144, in get_ticket_by_id
return Ticket.objects.filter(id=ticket_id).first()
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 564, in first
objects = list((self if self.ordered else self.order_by('pk'))[:1])
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 250, in __iter__
self._fetch_all()
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 1118, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/usr/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 899, in execute_sql
raise original_exception
OperationalError: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
这是我在 PostgreSQL 日志文件中得到的:
LOG: could not receive data from client: Connection reset by peer
关于这个问题的一些想法?我认为如果连接保持时间过长(> CONN_MAX_AGE),应该关闭conn并设置为None,但实际上连接不是null而是已经关闭