2012-01-13 60 views
3

这里是tasks.py无法运行芹菜教程

from celery.task import task 

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

这里是celeryconfig.py

print 'importing ' + __file__ 
BROKER_URL = "amqp://guest:[email protected]: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状态,我的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

您是否将add方法设置为任务?一个变体的这样做会用装饰:

from celery.decorators import task 

@task 
def add(): 
    pass 

也可以大家分享你tasks.py

+0

从celery.task导入任务 @task 高清加(X,Y): 返回X + Y – onfire 2012-01-13 08:55:08

+2

我增加了装饰。实际上,我只是从教程中复制了该文件。 – onfire 2012-01-13 08:58:14