避免芹菜经纪人的重复任务

ari*_*ikg 5 celery

我想使用celery configuration\api创建以下流程:

  • 发送TaskA(argB)仅当芹菜队列没有任何TaskA(argB)已挂起时

可能吗?怎么样?

Árn*_*son 9

您可以通过某种记忆方式让您的工作了解其他任务。如果您使用缓存控制键(redis,memcached,/tmp,任何方便的),您可以使执行依赖于该键。我以 redis 为例。

from redis import Redis

@app.task
def run_only_one_instance(params):
    try:
        sentinel =  Redis().incr("run_only_one_instance_sentinel")
        if sentinel == 1:
            #I am the legitimate running task
            perform_task()
        else:
            #Do you want to do something else on task duplicate?
            pass
        Redis().decr("run_only_one_instance_sentinel")
    except Exception as e:
        Redis().decr("run_only_one_instance_sentinel")
        # potentially log error with Sentry?
        # decrement the counter to insure tasks can run
        # or: raise e
Run Code Online (Sandbox Code Playgroud)


srj*_*srj 1

我想不出办法,只能

  1. 通过检索所有正在执行和计划的任务celery inspect

  2. 迭代它们以查看您的任务是否在那里。

检查这个SO问题,看看第一点是如何完成的。

祝你好运

  • 这并没有回答问题,而且它给出的解决方案也不实用。 (2认同)