简体   繁体   中英

Celery KeyError when i try to execute task

In accordance with the 'first steps with Celery' I have the following structure:

Celery_

  • config
    • celery.py
    • celeryconfig.py
  • tasks
    • tasks.py
  • run_tasks.py

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

run_tasks.py:

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

This is a very simple configuration, but when I run run_tasks.py, the console displays an error:

.... 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'

Help me please. What's wrong?

You need to import your tasks in 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM