2014-09-18 64 views
0

我有對我試圖從它的聲明在同一個模塊叫我的Django項目申報了芹菜任務中聲明眼下,它看起來像如下:Django的:調用芹菜任務同一模塊

# myapp.admin.py 

from myproject.celery import app as celery_app 

@celery_app.task(name='myapp.admin.add') 
def add(x, y): 
    time.sleep(10000) 
    return x + y 

def my_custom_admin_action(modeladmin, request, queryset): 
    add.delay(2, 4) 

# action later declared in the ModelAdmin 

知道芹菜有時會因相對進口而變得複雜,因此我指定了名稱。我甚至增加了以下我settings.py

CELERY_IMPORTS = ('myapp.admin',) 

但是當我嘗試使用管理動作,我得到我的manage.py celeryd輸出以下消息:

[2014-09-18 14:58:25,413: ERROR/MainProcess] Received unregistered task of type 'myapp.admin.add'. 
The message has been ignored and discarded. 

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. 

Traceback (most recent call last): 
    File "/Users/JJ/.virtualenvs/TCJ/lib/python2.7/site-packages/celery/worker/consumer.py", line 455, in on_task_received 
    strategies[name](message, body, 
KeyError: 'myapp.admin.add' 

什麼我錯在這裏做什麼?我甚至試圖在from . import add這個行動中進口,但這似乎沒有幫助。

回答

1

芹菜不挑選你的add任務。解決這個問題的另一種方法是修改芹菜的實例。

的myproject/celery.py芹菜 變化實例

app = Celery('name', backend='your_backend', broker='your_broker') 

app = Celery('name', backend='your_backend', broker='your_broker', 
      include['myapp.admin',])