本文介绍了Django超过了Postgres的最大连接数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了Django应用程序问题,该应用程序在通过 Gunicorn 和异步 eventlet 工作人员。当达到连接限制时,应用程序将开始返回 500 错误,直到可以建立新连接为止。

I am experiencing a problem with a Django application that is exceeding the maximum number of simultaneous connections (100) to Postgres when running through Gunicorn with async eventlet workers. When the connection limit it reached the application starts returning 500-errors until new connections can be established.

这是我的数据库配置:

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': 'django',
        'USER': 'django',
        'HOST': 'postgres',
        'PORT': 5432,
        'CONN_MAX_AGE': 60,
    }
}

这是启动Gunicorn的方式:

This is how Gunicorn is started:

gunicorn --bind 0.0.0.0:8080 --worker-class eventlet --workers 5 myapp.wsgi:application

这些是已安装的软件包:

These are the installed packages:


  • djano v1.7

  • gunicorn v19.3

  • eventlet v0.17

  • psycopg2 v2.6

  • djano v1.7
  • gunicorn v19.3
  • eventlet v0.17
  • psycopg2 v2.6

Django是否无法通过HTTP重用连接与Gunicorn工人一起跑步时有什么要求?

Is Django not able to reuse connections across HTTP requests when running with Gunicorn workers? Is some kind of 3rd party database connection pool my only option here?

更新15-03-23:似乎存在某种问题,这是我的唯一选择吗? CONN_MAX_AGE 和异步Gunicorn工作人员。连接确实是持久的,但从未在任何顺序请求中重用,如。将 CONN_MAX_AGE 设置为 0 会强制Django在请求结束时关闭连接,以防止未使用的持久连接形式摆放。 p>

Update 15-03-23: There appears to be a problem with CONN_MAX_AGE and async Gunicorn workers. Connections are indeed persistent but never reused in any sequential requests as noted in this post. Setting CONN_MAX_AGE to 0 forces Django to close connections when the request ends, preventing unused persistent connections form laying around.

推荐答案

Django没有数据库连接池。看看PgBouncer。它是一个轻量级的连接池,易于设置和配置:

Django does no database connection pooling. Have a look at PgBouncer. It is a lightweight connection pooled that is easy to setup and configure: https://wiki.postgresql.org/wiki/PgBouncer

简而言之:您的django应用程序已连接到PgBouncer,并且具有重复使用的Postgres连接池,因此永远不会有最大连接限制超出了。

In short: Your django app connects to PgBouncer and it has a pool of connections to Postgres that it reuses, so the maximum connection limit is never exceeded.

这篇关于Django超过了Postgres的最大连接数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:09
查看更多