gpt4 book ai didi

django - celery worker 无法使用 Django 连接到 dockerized redis 实例

转载 作者:可可西里 更新时间:2023-11-01 11:12:27 27 4
gpt4 key购买 nike

目前有一个 dockerized django 应用程序,打算使用 Celery 来处理长时间运行的任务。但是 Docker-compose up 失败并出现以下错误:

[2018-12-17 17:25:59,710: ERROR/MainProcess] consumer: Cannot 
connect to redis://redis:6379//: Error -2 connecting to redis:6379.
Name or service not known..

在 SOF 上有一些类似的问题,但它们似乎都集中在 settings.py 中的 CELERY_BROKER_URL,我相信我已经正确设置如下

CELERY_BROKER_URL = 'redis://redis:6379'
CELERY_RESULT_BACKEND = 'redis://redis:6379'

我的 docker-compose.yml :

db:
image: postgres:10.1-alpine
restart: unless-stopped
volumes:
- postgres_data:/var/lib/postgresql/data/
networks:
- dsne-django-nginx
django: &python
restart: unless-stopped
build:
context: .
networks:
- dsne-django-nginx
volumes:
- dsne-django-static:/usr/src/app/static
- dsne-django-media:/usr/src/app/media
ports:
- 8000:8000
depends_on:
- db
- redis
- celery_worker
nginx:
container_name: dsne-nginx
restart: unless-stopped
build:
context: ./nginx
dockerfile: nginx.dockerfile
networks:
- dsne-django-nginx
volumes:
- dsne-django-static:/usr/src/app/static
- dsne-django-media:/usr/src/app/media
- dsne-nginx-cert:/etc/ssl/certs:ro
- /etc/ssl/:/etc/ssl/
- /usr/share/ca-certificates/:/usr/share/ca-certificates/
ports:
- 80:80
- 443:443
depends_on:
- django
redis:
image: redis:alpine
celery_worker:
<<: *python
command: celery -A fv1 worker --loglevel=info
ports: []
depends_on:
- redis
- db
volumes:
postgres_data:
dsne-django-static:
driver: local
dsne-django-media:
driver: local
dsne-nginx-cert:
networks:
dsne-django-nginx:
driver: bridge

初始化.py :

from .celery import fv1 as celery_app

__all__ = ('celery_app',)

celery .py :

    from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
import fv1
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'fv1.settings')

app = Celery('fv1')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))

我哪里出错了,为什么我的 celery worker 不能连接到 Redis?

最佳答案

你的 redis 容器没有列出 6379 端口

关于django - celery worker 无法使用 Django 连接到 dockerized redis 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53820359/

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