首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么芹菜任务不起作用?

为什么芹菜任务不起作用?
EN

Stack Overflow用户
提问于 2018-06-02 20:56:44
回答 1查看 1.1K关注 0票数 0

在我的Django项目中,我需要定期运行celery任务。我用的是芹菜4+ Redis。

首先我运行redis-server,然后使用next命令:

代码语言:javascript
复制
$ celery -A TestProject worker -l info
$ celery -A TestProject beat -l info

第一个命令引发下一个错误:

代码语言:javascript
复制
The full contents of the message body was:
b'[[], {}, {"errbacks": null, "chord": null, "chain": null, "callbacks": null}]' (77b)
Traceback (most recent call last):
  File "/home/ubuntu/enjoy_jumping/lib/python3.5/site-packages/celery/worker/consumer/consumer.py", line 557, in on_task_received
    strategy = strategies[type_]
KeyError: 'profile.tasks.amount_counting'

celery.py (文件与settings.py文件位于同一目录中)

代码语言:javascript
复制
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

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

app = Celery('TestProject')

# 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()


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

init.py: (文件与settings.py文件在同一目录中)

代码语言:javascript
复制
from __future__ import absolute_import, unicode_literals

# This will make sure the app is always imported when Django starts so that shared_task will use this app.
from .celery import app as celery_app

__all__ = ['celery_app']

settings.py:

代码语言:javascript
复制
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/Almaty'

# Other Celery settings
CELERY_BEAT_SCHEDULE = {
    'amount-counting': {
        'task': 'profile.tasks.amount_counting',
        'schedule': timedelta(seconds=60), 
    }
}

tasks.py: ( profile应用程序文件夹中的文件)

代码语言:javascript
复制
from __future__ import absolute_import, unicode_literals
from celery import task


@task()
def amount_counting():
    # Code here
EN

回答 1

Stack Overflow用户

发布于 2018-06-04 02:24:10

最后我找到了解决方案。我编辑celery.py文件:

代码语言:javascript
复制
import os
import sys
from celery import Celery
from celery._state import _set_current_app
import django
from django.conf import settings


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TestProject.settings')
app = Celery('TestProject')
app.config_from_object('django.conf:settings', namespace='CELERY')
_set_current_app(app)
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../TestProject')))
django.setup()
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50657150

复制
相关文章

相似问题

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