我有一个具有某个参数的任务,我想知道是否有一个具有相同参数的任务。我有以下内容:
@task
def some_task(id):
some_task.update_state(state="PROGRESS", meta={"id": id})
some_action_by_id(id)
但我想这样:
@task
def some_task(id):
if !check_task(id):
some_task.update_state(state="PROGRESS", meta={"id": id})
some_action_by_id(id)
我怎样才能做到这一点?
您的意思可能是您希望每个唯一id在每个时刻只运行一个"some_task"。所以你必须实现一个锁定机制。请看这里。芹菜和Redis很搭!
我的解决方案是:
from celery.task.control import inspect
from celery.result import AsyncResult
def get_same_task(aTaskName, aArgs, aHosts):
for jobs in aHosts.values():
for job in jobs:
if job['name'] == aTaskName and job['args'] == str(aArgs):
return job['id']
return None
class IgnoreSameArgumentsTask(Task):
abstract = True
inspect = inspect()
def delay(self, *args, **kwargs):
vHosts_Jobs = self.inspect.active()
vTaskId = get_same_task(self.name, args, vHosts_Jobs)
if vTaskId != None:
return AsyncResult(vTaskId)
else:
return super(IgnoreSameArgumentsTask, self).delay(*args, **kwargs)
@celery.task(base=IgnoreSameArgumentsTask)
def add(x, y):
sleep(x+y)
return x + y