【发布时间】:2021-05-04 05:44:38
【问题描述】:
我对 psycopg2 有疑问。它无法执行某些查询。我跟踪了异常,它说:django.db.utils.InterfaceError: cursor already closed
我已经在网上看到过一些类似的案例,尝试了所有的建议,但没有运气。
我使用 Django Rest Framework,在请求某些端点时会导致错误,只是在某些请求中。我监控了 error.log 文件,我看到 15-20 个请求因 HTTP 500 错误而失败。
这个问题有解决办法吗?
软件版本:
djangorestframework == 3.12.1
django == 3.0.5
Python 3.8
这是我在服务器上运行的uswgi.ini 文件。 Socket 被 NGINX 使用。
[uwsgi]
project = DjangoProject
base = /opt/django-project
chdir = %(base)
module = %(project).wsgi:application
home = %(base)/venv
gid = www-data
uid = www-data
master = true
processes = 5
socket = /tmp/%(project).sock
chmod-socket = 664
vacuum = true
harakiri = 60
max-requests = 10000
我试图通过增加与 Postgresql 的连接来解决问题。但没有改变,错误仍然存在。
来自postgresql.conf 文件的片段:
max_connections = 2000
shared_buffers = 800MB
完整的堆栈跟踪:
Internal Server Error: /api/users/
Traceback (most recent call last):
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py", line 97, in inner
return func(*args, **kwargs)
psycopg2.InterfaceError: cursor already closed
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/django-project/venv/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/opt/django-project/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/opt/django-project/venv/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/django-project/venv/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/viewsets.py", line 125, in view
return self.dispatch(request, *args, **kwargs)
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 497, in dispatch
self.initial(request, *args, **kwargs)
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 414, in initial
self.perform_authentication(request)
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/views.py", line 324, in perform_authentication
request.user
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/request.py", line 227, in user
self._authenticate()
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/request.py", line 380, in _authenticate
user_auth_tuple = authenticator.authenticate(self)
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/authentication.py", line 193, in authenticate
return self.authenticate_credentials(token)
File "/opt/django-project/venv/lib/python3.8/site-packages/rest_framework/authentication.py", line 198, in authenticate_credentials
token = model.objects.select_related('user').get(key=key)
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py", line 411, in get
num = len(clone)
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py", line 258, in __len__
self._fetch_all()
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/query.py", line 57, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1184, in execute_sql
return list(result)
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1583, in cursor_iter
for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1583, in <lambda>
for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py", line 97, in inner
return func(*args, **kwargs)
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/opt/django-project/venv/lib/python3.8/site-packages/django/db/utils.py", line 97, in inner
return func(*args, **kwargs)
django.db.utils.InterfaceError: cursor already closed
【问题讨论】:
-
添加连接并不能解决这个问题。问题是连接上的光标,例如:
con = psycopg2.connect(dsn) cur = con.cursor()代码中的某些内容是创建一个游标,然后执行cursor.close(),然后再次尝试使用关闭的游标。 -
问题解决了吗?我也面临同样的问题,找不到答案。
标签: python django postgresql django-rest-framework psycopg2