有没有一种方法可以在具有特定任务 ID 的特定任务成功或使用 Celery for Python 失败时立即收到通知?

Is there a way to receive a notification as soon as a certain task with a certain task id is successful or fails using Celery for Python?

我想知道是否有一种方法可以使用 python celery 来监控任务是完成还是失败。我有一个事件要根据特定任务的结果启动。

你可以运行你的任务作为芹菜@shared_task,里面有一个try except块:

@shared_task
def my_task(input1, input2, ...):
    Setting up...
    try:
        Do stuff
        fire_success_event() <- Your success event
    except Exception:
        The above stuff failed
        fire_fail_event() <- your fail event
        return 1 <- fail
    return 0 <- success

祝你好运:)