无法运行Celery教程

3 投票
1 回答
1736 浏览
提问于 2025-04-17 10:09

这是 tasks.py 文件

from celery.task import task

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

这是 celeryconfig.py 文件

print 'importing ' + __file__
BROKER_URL = "amqp://guest:guest@localhost:5672//"
CELERY_RESULT_BACKEND = "amqp"
CELERY_IMPORTS = ("tasks", )

这是我运行的文件。 tasks.py:

from tasks import add
result = add.delay(4, 4)
print result.wait()

程序在等待的方法里卡住了。

Celeryd 打印出了以下错误:

Did you remember to import the module containing this task?
Or maybe you are using relative imports?
Please see http://bit.ly/gLye1c for more information.

The full contents of the message body was:
{'retries': 0, 'task': 'tasks.add', 'args': (4, 4), 'expires': None, 'eta': None
, 'kwargs': {}, 'id': '8c973638-4a87-4afa-8a78-958153066215'}
Traceback (most recent call last):
  File "d:\python26\lib\site-packages\celery-2.4.5-py2.6.egg\celery\worker\consu
mer.py", line 427, in receive_message
    eventer=self.event_dispatcher)
  File "d:\python26\lib\site-packages\celery-2.4.5-py2.6.egg\celery\worker\job.p
y", line 297, in from_message
    on_ack=on_ack, delivery_info=delivery_info, **kw)
  File "d:\python26\lib\site-packages\celery-2.4.5-py2.6.egg\celery\worker\job.p
y", line 261, in __init__
    self.task = registry.tasks[self.task_name]
  File "d:\python26\lib\site-packages\celery-2.4.5-py2.6.egg\celery\registry.py"
, line 66, in __getitem__
    raise self.NotRegistered(key)
NotRegistered: 'tasks.add'

当我运行 celeryd.py status 时,发现 tasks.add 不在这里。

D:\Python26\Lib\site-packages\celery-2.4.5-py2.6.egg\celery\bin>celeryctl.py in
pect registered
<- registered
-> onfirenbpc: OK
    * celery.backend_cleanup
    * celery.chord
    * celery.chord_unlock
    * celery.ping

我在 Windows 和 Linux 上都运行过,问题是一样的。

有没有人知道这是为什么?

1 个回答

1

你有没有把你的 add 方法设置为任务?一种实现这个的方式是使用装饰器:

from celery.decorators import task

@task
def add():
    pass

撰写回答