python - 正确设置 django redis celery 和 celery beats

标签 python django celery celerybeat

我一直在尝试设置 django + celery + redis + celery_beats,但这给我带来了麻烦。文档非常简单,但是当我运行 django 服务器、redis、celery 和 celery beats 时,没有打印或记录任何内容(我所有的测试任务都会记录一些东西)。

这是我的文件夹结构:

- aenima 
 - aenima
   - __init__.py
   - celery.py

 - criptoball
   - tasks.py

celery.py 看起来像这样:

from __future__ import absolute_import, unicode_literals
import os
from django.conf import settings
from celery import Celery


# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'aenima.settings')

app = Celery("criptoball")
app.conf.broker_url = 'redis://localhost:6379/0'

# 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(lambda: settings.INSTALLED_APPS)
app.conf.timezone = 'UTC'

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

app.conf.beat_schedule = {
    'test-every-30-seconds': {
        'task': 'tasks.test_celery',
        'schedule': 30.0,
        'args': (16, 16)
    }, }

tasks.py 看起来像这样:

from __future__ import absolute_import, unicode_literals
from datetime import datetime, timedelta
from celery import shared_task
import logging

from django_celery_beat.models import PeriodicTask, IntervalSchedule

cada_10_seg = IntervalSchedule.objects.create(every=10, period=IntervalSchedule.SECONDS)

test_celery_periodic = PeriodicTask.objects.create(interval=cada_10_seg, name='test_celery', task='criptoball.tasks.test_celery',
expires=datetime.utcnow()+timedelta(seconds=30))

@shared_task
def test_celery(x, y):
    logger = logging.getLogger("AENIMA")
    print("EUREKA")
    logger.debug("EUREKA")

这是 django_celery_beat文档

不确定我错过了什么。当我运行时

celery -A aenima beat -l debug --scheduler django_celery_beat.schedulers:DatabaseScheduler

celery -A aenima worker -l debug

redis-cli ping PONG

django runserver 和 redis 服务器,我没有打印任何内容。

设置.py

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = TIME_ZONE
CELERY_IMPORTS = ('criptoball.tasks',)

到目前为止,还没有在 SO 中找到关于这个主题的任何权威答案。

我想解决所有问题,这个错误可能只是其中之一。非常感谢您的帮助!

编辑:

添加了 redis 设置,以不同方式声明了任务并提高了调试级别。现在错误是:

Received unregistered task of type u'tasks.test_celery'. The message has been ignored and discarded.

Did you remember to import the module containing this task? Or maybe you're using relative imports? KeyError: u'aenima.criptoball.tasks.test_celery'

我认为 Celery 的文档很差。

编辑 2 在尝试了一切之后,当我将所有任务放在同一个 celery.py 文件中时,它起作用了。 @shared_task 不起作用,必须使用 @app.task 。

最佳答案

我以前遇到过这些问题。这不是你的代码。一般是环境问题。 您应该在 virtualenv 下运行所有​​内容,添加一个包含特定包版本requirements.txt 文件。

关于 celery 4.xdjango 1.x 有一个已知问题,所以你应该考虑你正在使用的包。

This教程将详细解释如何使用 celery 构建 virtualenv

如果您能告诉我您的软件包版本,我可能会尝试以不同的方式提供帮助。

编辑:

我认为这与您经营 celery 的方式有关。如果我们解决了第一个问题,试试这个:

celery -A aenima.celery:app beat -l debug --scheduler django_celery_beat.schedulers:DatabaseScheduler

celery -A aenima.aenima.celery:app beat -l debug --scheduler django_celery_beat.schedulers:DatabaseScheduler

您遇到的最新错误与您的模块发现有关。 先试试吧。

关于python - 正确设置 django redis celery 和 celery beats,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48598870/

相关文章:

django - 在 DefaultRouter 的根 api View 上订购项目

python - 属性错误 : 'module' object has no attribute 'celery'

python - ACCESS_REFUSED-使用身份验证机制AMQPLAIN拒绝登录。有关详细信息,请参阅代理日志文件

python - 在 Django 中查询多对多关系

python - 在Python中同时对URL发出多个POST请求

Django-celery + redis 不显示任务

Python 使用 Seaborn 绘制 Pandas SQL 数据框

python - 如何从具有预设条件的数据框中随机抽取一定数量的行?

python - 在图形而不是轴的坐标系中设置轴标签

python - 从路径导入 Pandas