Celery连接到Rabbitmq服务器而不是Redis服务器

时间:2018-11-21 06:51:47

标签: django celery background-process redis-server

我有一个Django应用程序,我想对其进行配置以运行后台任务。

包装:

  1. 芹菜== 4.2.1

  2. Django == 2.1.3

  3. Python == 3.5

  4. Redis-server == 3.0.6

settings.py 文件中的芹菜配置为:

?

celery.py 文件:

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'),
    },
}

我跑步时: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))

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

celery -A project worker -l info -B -E

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

  

问题:

     
      
  1. 芹菜配置有什么问题?

  2.   
  3. 谁能推荐一种运行定期后台任务的方法?

  4.   

**注意:我已经尝试实现超级用户,但是超级用户似乎与python3不兼容,因此无法对其进行配置。

4 个答案:

答案 0 :(得分:1)

broker url changed in v4的设置。它应该是BROKER_URL,而不是CELERY_BROKER_URL

答案 1 :(得分:0)

要使芹菜与redis一起使用,您必须安装其他依赖项。

pip install -U "celery[redis]"

请通过celery documentation

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

尝试更改以下行,从

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

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