gpt4 book ai didi

django - FATAL : too many connections for role: Heroku/django, 仅在使用 ASGI 时

转载 作者:行者123 更新时间:2023-12-04 15:34:09 25 4
gpt4 key购买 nike

我知道还有其他类似的问题,但我相信我已经解决了所有这些问题,但没有一个能够解决我的问题。当我使用 WSGI 服务器时一切正常。它仅在我使用 ASGI 服务器时发生,但是我必须使用 ASGI 服务器,因为没有它我的项目功能将受到限制。我正在使用 postgres 数据库。

此外,我相信在本地主机上运行项目时没有问题,因为我检查了与我的数据库(在我的机器上用于开发)的连接数,我只看到了 1 个。这 1 个连接也不是来自django 项目到我的数据库,但它是我自己的连接,我用它来显式访问数据库。我不知道为什么从我的项目到数据库连一个连接都没有。

Procfile

web: daphne aretheycoming.asgi:application --port $PORT --bind 0.0.0.0 -v2
aretheycomingworker: python manage.py runworker --settings=aretheycoming.settings -v2
asgi.py

import os
import django
from channels.routing import get_default_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aretheycoming.settings")
django.setup()
application = get_default_application()
settings.py

import django_heroku

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = hidden

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'channels',
.
.
.
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'aretheycoming.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'aretheycoming.wsgi.application'
ASGI_APPLICATION = 'aretheycoming.routing.application'

CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 6379)],
"hosts": [os.environ.get('REDIS_URL', 'redis://localhost:6379')]
},
},
}


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': hidden,
'USER': hidden,
'PASSWORD': hidden,
'HOST': '127.0.0.1',
'PORT': '5432',
}
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]

# Activate Django-Heroku.
django_heroku.settings(locals())

我尝试使用 django.db.close_all_connections() 但它没有用,而且项目在 wsgi 服务器上运行良好,所以我认为这不是问题所在。我对这些东西没有很好的理解,所以如果你能再多解释一下,我将不胜感激。

我在 Heroku 上设置了环境变量 ASGI_THREADS = 1。

最佳答案

https://devcenter.heroku.com/articles/postgres-logs-errors#fatal-too-many-connections-for-role

为了实现异步,它同时建立了多个连接。这就是它使用多个连接的原因。

该用户遇到了同样的问题并提供了解决方案:Django/Heroku: FATAL: too many connections for role

关于django - FATAL : too many connections for role: Heroku/django, 仅在使用 ASGI 时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60339917/

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