【发布时间】:2018-07-16 14:04:29
【问题描述】:
我在 Heroku 上使用 Django 和 Django Rest Framework 以及 postgresql 和 redis 数据存储运行 Web 应用程序。我使用的是限制为 20 个连接的免费 postgresql 层。
这在过去不是问题,但最近我开始使用 django channels 2.0 和 daphne server(将我的 Procfile 从 gunicorn 切换到像 this tutorial 这样的 daphne),现在我遇到了各种各样的问题奇怪的问题。
最关键的是与数据库的连接保持打开状态,因此当应用程序运行时,连接数不断增加,直到达到 20 并给我以下错误消息:操作错误 - 'FATAL too many connections for role “用户名”'
然后我每次都必须手动进入 shell 并输入heroku pg:killall,这显然不是一个可行的解决方案,而且这是生产,所以我的用户无法访问站点并得到 500 个错误。非常感谢任何帮助。
我试过了:
将此添加到我在不同地方的不同视图中
from django.db import connections
conections.close_all()
for con in connections:
con.close()
我也尝试过SELECT * from pg_activity,看到了一堆东西,但不知道该怎么做:
【问题讨论】:
标签: django postgresql heroku django-rest-framework django-channels