Sto*_*ber 6 python django postgresql psycopg2 django-rest-framework
我的 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我在服务器上运行的文件。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
Run Code Online (Sandbox Code Playgroud)
我尝试通过增加与 Postgresql 的连接来解决问题。但没有改变,错误仍然存在。文件片段postgresql.conf:
max_connections = 2000
shared_buffers = 800MB
Run Code Online (Sandbox Code Playgroud)
完整的堆栈跟踪:
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
Run Code Online (Sandbox Code Playgroud)
这很可能是由客户端关闭连接引起的。有关详细说明,请参阅https://forum.djangoproject.com/t/db-connection-filled-by-uwsgi-in-the-middle-of-handling-a-request/9875 。
| 归档时间: |
|
| 查看次数: |
4557 次 |
| 最近记录: |