我一直在尝试设置 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.x
和 django 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/