在看了很多关于和弦回调未执行的文章并尝试了他们的解决方案之后,我仍然无法让它工作。事实上,由于某种原因,chord_unlock 方法也没有被执行。
芹菜.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()
我的worker日志文件的输出如下
$ 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)
和弦文档下有一个重要注释部分,内容如下:
和弦中使用的任务必须不忽略其结果。实际上,这意味着您必须启用 CELERY_RESULT_BACKEND 才能使用和弦。此外,如果您的配置中 CELERY_IGNORE_RESULT 设置为 True,请确保使用 ignore_result=False 定义要在和弦中使用的各个任务。这适用于任务子类和修饰任务。
即使我遵循了建议,和弦回调也不会执行,但可能对其他人有帮助。
编辑:
找到了我的情况!和弦并不完全支持在所有情况下指定队列名称: https://github.com/celery/celery/issues/2085
当我的任务签名与流程提供的参数不匹配时,接受
*args
可能有助于调试此类问题。
对我来说,回调函数从未执行,因为芹菜结果过期时间设置得很低,所以除了其他评论中所述的
CELERY_RESULT_BACKEND
和ignore_result=False
之外,您应该检查此设置以防万一CELERY_RESULT_EXPIRES
文档链接:https://docs.celeryq.dev/en/stable/userguide/configuration.html#std-setting-result_expires