首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Celery连接到rabbitmq-server而不是redis-server

Celery连接到rabbitmq-server而不是redis-server
EN

Stack Overflow用户
提问于 2018-11-21 14:51:47
回答 3查看 2.4K关注 0票数 5

我有一个Django应用程序,我想配置它芹菜来运行后台任务。

包:

  • Django==2.1.3

  • Python==3.5

  • Redis-server==3.0.6

  1. celery==4.2.1

settings.py file中芹菜的配置为:

CELERY_BROKER_URL = 'redis://localhost:6379'

CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Asia/Kolkata'
CELERY_BEAT_SCHEDULE = {
    'task-number-one': {
            'task': 'app.tasks.task_number_one',
            'schedule': crontab(minute='*/1'),
    },
}

celery.py文件:

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

app = Celery('project')

# Using a string here means the worker don'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')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


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

当我运行时:celery -A project worker -l info -B -E

它指向rabmmitmq服务器,而不是指向redis-server,如下所示:

 -------------- celery@user-desktop v4.2.1 (windowlicker)
---- **** ----- 
--- * ***  * -- Linux-4.15.0-39-generic-x86_64-with-Ubuntu-18.04-bionic 2018-11-21 12:04:51
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         project:0x7f8b80f78d30
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     redis://localhost:6379/
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: ON
--- ***** ----- 
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]
  . app.tasks.task_number_one
  . project.celery.debug_task

[2018-11-21 12:04:51,741: INFO/Beat] beat: Starting...

同样的情况也发生在生产环境中。在生产环境中,我已经使用Gunicorn和Nginx部署了Django应用程序,现在我想实现一些方法来运行后台任务,因为django-crontab包不起作用。

问题:

celery configuration?

  • Could有什么问题?
  1. 请推荐一种运行周期性后台任务的方法?

**注意:我曾尝试实现supervisor,但supervisor似乎与python3不兼容,因此无法配置。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-11-22 13:51:03

它应该是BROKER_URL而不是CELERY_BROKER_URL

票数 8
EN

Stack Overflow用户

发布于 2019-07-14 19:10:19

如果您从芹菜官网https://docs.celeryproject.org/en/latest/django/first-steps-with-django.html复制了celery.py的内容

尝试更改以下行,从

app.config_from_object('django.conf:settings', namespace='CELERY')

app.config_from_object('django.conf:settings', namespace='')

票数 7
EN

Stack Overflow用户

发布于 2019-06-26 04:54:00

BROKER_URL = 'redis://localhost:6379'替换CELERY_BROKER_URL = 'redis://localhost:6379'。这对我很有效。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53406661

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档