本文介绍了正确设置Django Redis芹菜和芹菜节拍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试设置django + celery + redis + celery_beats,但这给我带来了麻烦。该文档非常简单,但是当我运行django服务器,redis,芹菜和芹菜节拍时,什么也不会打印或记录(我的所有测试任务都会对其进行记录)。

I have being trying to setup django + celery + redis + celery_beats but it is giving me trouble. The documentation is quite straightforward, but when I run the django server, redis, celery and celery beats, nothing gets printed or logged (all my test task does its log something).

这是我的文件夹结构:

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

 - criptoball
   - tasks.py

celery.py看起来像这样:

celery.py looks like this:

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看起来像这样:

and tasks.py looks like this:

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")

这是文档

不确定我想念的是什么。当我运行

Not sure what am I missing. When I run

芹菜-灌肠工人-l调试

celery -A aenima worker -l debug

重新分发
PONG

redis-cli ping PONG



django runserver和redis服务器,我什么也没打印。

django runserver and redis server, I get nothing printed.

settings.py

settings.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',)

天堂到目前为止,在这个话题上还没有找到任何权威的答案。

Haven't found any authorative answer to this topic in SO so far.

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

I would like to solve it all, this error may be just one of many. Thanks a lot for your help!

编辑:

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

Added settings for redis, declared the the task differently and increased debug level. Now the error is:

您是否还记得导入包含此任务的模块?还是您使用相对进口商品的

KeyError:u'aenima.criptoball.tasks.test_celery'

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的文档很差。

I believe Celery's documentation is poor.

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

EDIT 2After trying everything, it worked when I put all the tasks inside the same celery.py file. the @shared_task doesn't work, had to use @app.task .

推荐答案

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

I had those issues before. It's not your code. It's usually a problem with the environment.You should run everything under virtualenv, adding a requirements.txt file with the specific package versions.

关于芹菜4.x 和 django 1.x ,因此您应该考虑使用的软件包。

There is a know issue regarding celery 4.x and django 1.x, so you should consider the packages you are using.

教程将在详细说明如何使用芹菜构建 virtualenv

This tutorial will explain in detail how to build virtualenv with celery.

如果您能告诉我您的软件包版本,我可能会尝试并提供帮助

If you can tell me your packages versions I might try and help in a different way.

编辑:

我认为这与方法有关你经营芹菜。如果我们解决了第一个问题,请尝试以下操作:

I think its something about the way you run your celery. If we fixed the first problem, try play with this:

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

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

The latest error you are getting is something to do with your module discovery.Try it first.

这篇关于正确设置Django Redis芹菜和芹菜节拍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 09:33