当我尝试执行任务时出现 Celery KeyError

Celery KeyError when i try to execute task

按照'first steps with Celery' 我有以下结构:

芹菜_

celery.py:

from celery import Celery
from config import celeryconfig
app = Celery('tasks', backend='rpc://', broker='pyamqp://')
app.config_from_object(celeryconfig)

celeryconfig.py

broker_url = 'pyamqp://'
result_backend = 'rpc://'
task_serializer = 'json'
result_serializer = 'json'
accept_content = ['json']
timezone = 'Europe/Oslo'
enable_utc = True

task_routes = {
'tasks.add': 'low-priority',
}
task_annotations = {
   'tasks.add': {'rate_limit': '10/m'}
}

tasks.py:

from config.celery import app


@app.task
def add(x, y):
return x + y

运行_tasks.py:

from tasks.tasks import add
res = add.delay(4,4)
a = res.get()
print(a)

这是一个很简单的配置,但是当我运行运行_tasks.py的时候,控制台显示错误:

.... Traceback (most recent last call last): File "/usr/lib/python3/dist-packages/celery/worker/consumer.py", line 456, in on_task_received strategies [name] (message, body, KeyError: 'tasks.tasks.add'

请帮帮我。怎么了?

您需要在 celery.py

中导入您的任务
from celery import Celery
from config import celeryconfig
app = Celery('tasks.tasks', backend='rpc://', broker='pyamqp://')
app.config_from_object(celeryconfig)
import tasks.tasks