2013-12-12 54 views
1

在看過很多關於和絃回調沒有執行和嘗試他們的解決方案的文章後,我仍然無法使它工作。實際上,chord_unlock方法由於某種原因也沒有得到執行。芹菜沒有運行和絃回調

celery.py

from __future__ import absolute_import 
from celery import Celery 

app = Celery('sophie', 
      broker='redis://localhost:6379/2', 
      backend='redis://localhost:6379/2', 
      include=['sophie.lib.chord_test']) 

app.conf.update(
    CELERY_ACCEPT_CONTENT=["json"], 
    CELERY_TASK_SERIALIZER="json", 
    CELERY_TRACK_STARTED=True, 
    CELERYD_PREFETCH_MULTIPLIER=1, # NO PREFETCHING OF TASKS 
    BROKER_TRANSPORT_OPTIONS = { 
     'priority_steps': [0, 1]  # ALLOW ONLY 2 TASK PRIORITIES 
    } 
) 

if __name__ == '__main__': 
    app.start() 

chord_test.py

from __future__ import absolute_import 
from sophie.celery import app 
from celery import chord 

@app.task(name='sophie.lib.add') 
def add(x, y): 
    return x + y 

@app.task(name='sophie.lib.tsum') 
def tsum(numbers): 
    return sum(numbers) 

if __name__ == '__main__': 
    tasks = [add.s(100, 100), add.s(200, 200)] 
    chord(tasks, tsum.s()).apply_async() 

我的工人日誌文件的輸出如下

$ celery worker -l info --app=sophie.celery -n worker1.%h 

-------------- [email protected] v3.1.6 (Cipater) 
---- **** ----- 
--- * *** * -- Linux-3.0.0-12-server-x86_64-with-Ubuntu-11.10-oneiric 
-- * - **** --- 
- ** ---------- [config] 
- ** ---------- .> broker:  redis://localhost:6379/2 
- ** ---------- .> app:   sophie:0x3554250 
- ** ---------- .> concurrency: 1 (prefork) 
- *** --- * --- .> events:  OFF (enable -E to monitor this worker) 
-- ******* ---- 
--- ***** ----- [queues] 
-------------- .> celery   exchange=celery(direct) key=celery 


[tasks] 
    . sophie.lib.add 
    . sophie.lib.tsum 

[2013-12-12 19:37:26,499: INFO/MainProcess] Connected to redis://localhost:6379/2 
[2013-12-12 19:37:26,506: INFO/MainProcess] mingle: searching for neighbors 
[2013-12-12 19:37:27,512: INFO/MainProcess] mingle: all alone 
[2013-12-12 19:37:27,527: WARNING/MainProcess] [email protected] ready. 
[2013-12-12 19:37:29,723: INFO/MainProcess] Received task: sophie.lib.add[b7d504c1-217f-43a9-b57e-86f0fcbdbe22] 
[2013-12-12 19:37:29,734: INFO/MainProcess] Task sophie.lib.add[b7d504c1-217f-43a9-b57e-86f0fcbdbe22] succeeded in 0.009769904 
00522s: 200 
[2013-12-12 19:37:29,735: INFO/MainProcess] Received task: sophie.lib.add[eb01a73e-f6c8-401d-8049-6cdbc5f0bd90] 
[2013-12-12 19:37:29,737: INFO/MainProcess] Task sophie.lib.add[eb01a73e-f6c8-401d-8049-6cdbc5f0bd90] succeeded in 0.001446505 
00442s: 400 

沒有chord_unlock被稱爲可言。有些多個輸出透露進一步的情況下:

$ sudo pip freeze | egrep 'celery|kombu|billiard' 
billiard==3.3.0.12 
celery==3.1.6 
kombu==3.0.7 

$ uname -a 
Linux vagrant-ubuntu-11 3.0.0-12-server #20-Ubuntu SMP Fri Oct 7 16:36:30 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux 

$ redis-server --version 
Redis server version 2.2.11 (00000000:0) 
+0

您是否曾經弄明白這一點?我現在有類似的問題。 – devrelm

回答

2

chords documentation使用和絃的語法如下:

result = chord(header)(callback) 

所以,你可以這樣做:

chord(tasks)(tsum.s()) 
0

有下一個Important Notes節和絃文件說:

和絃內使用的任務必須而不是忽略其結果。實際上,這意味着您必須啓用一個CELERY_RESULT_BACKEND才能使用和絃。此外,如果CELERY_IGNORE_RESULT設置爲True在您的配置中,請確保要在和絃內使用的各個任務使用ignore_result = False定義。這適用於任務子類和裝飾任務。

即使我遵循建議,和絃回調不會執行,但可能會幫助其他人。

編輯:

發現這是什麼在我的情況!在所有情況下,和絃並不完全支持指定隊列名稱: https://github.com/celery/celery/issues/2085

+0

我知道這並不回答原來的問題,但可能會幫助一般芹菜和絃問題的人。 –