gpt4 book ai didi

Django/Heroku : FATAL: too many connections for role

转载 作者:行者123 更新时间:2023-12-04 12:45:06 32 4
gpt4 key购买 nike

因此,我刚刚通过Heroku(Hobby)和Postgres(trial)通过Channels 2.0 Daphne 2.2.0和asgi创建了一个网站。当我启动我的网站时,我单击了几页,出现500错误。我通过电子邮件收到的错误消息是FATAL: too many connections for role ..."
当我运行heroku pg:killall或等待足够长的时间时,我可以单击几下,直到错误消息再次出现。但是,当我运行heroku pg时,它显示Connections 0/20。有谁知道发生了什么事以及如何停止错误?我可能有两个打开的多个连接可能会持续一秒钟,但事实并非如此。

File "/app/.heroku/python/lib/python3.6/site-

packages/django/contrib/sessions/backends/base.py" in _get_session
191. return self._session_cache

During handling of the above exception ('SessionStore' object has no attribute '_session_cache'), another exception occurred:

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in ensure_connection
216. self.connect()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in connect
194. self.connection = self.get_new_connection(conn_params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py" in get_new_connection
168. connection = Database.connect(**conn_params)

File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py" in connect
130. conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

The above exception (FATAL: too many connections for role "polewdwynmvyyt"
) was the direct cause of the following exception:

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)

File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "./myproject/views.py" in home_page
8. print(request.session.get("first_name","Unknown")) #getter

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py" in get
66. return self._session.get(key, default)

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py" in _get_session
196. self._session_cache = self.load()

File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py" in load
34. expire_date__gt=timezone.now()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/manager.py" in manager_method
82. return getattr(self.get_queryset(), name)(*args, **kwargs)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in get
397. num = len(clone)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in __len__
254. self._fetch_all()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in _fetch_all
1179. self._result_cache = list(self._iterable_class(self))

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py" in __iter__
53. results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py" in execute_sql
1066. cursor = self.connection.cursor()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in cursor
255. return self._cursor()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in _cursor
232. self.ensure_connection()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in ensure_connection
216. self.connect()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py" in __exit__
89. raise dj_exc_value.with_traceback(traceback) from exc_value

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in ensure_connection
216. self.connect()

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/base/base.py" in connect
194. self.connection = self.get_new_connection(conn_params)

File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/postgresql/base.py" in get_new_connection
168. connection = Database.connect(**conn_params)

File "/app/.heroku/python/lib/python3.6/site-packages/psycopg2/__init__.py" in connect
130. conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

最佳答案

似乎没有重用连接和/或正在为每个请求创建一个新线程。

CONN_MAX_AGE上的dj_database_url.config(default=DATABASE_URL)中删除settings.py(如果使用的是dj_database_url)。

将环境变量ASGI_THREADS的线程数设置为低于.asgi文件或heroku网站->设置-> configVars上的连接限制。
如果使用daphne,则默认线程为CPU内核* 5,每个线程都有一个连接。

example.asgi文件:

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yourAppName.settings")
os.environ['ASGI_THREADS']="4"
django.setup()
application = get_default_application()

关于Django/Heroku : FATAL: too many connections for role,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51510259/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com