您可以通过某种记忆方式让您的工作了解其他任务。如果您使用缓存控制键(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)
我想不出办法,只能
通过检索所有正在执行和计划的任务celery inspect
迭代它们以查看您的任务是否在那里。
检查这个SO问题,看看第一点是如何完成的。
祝你好运
| 归档时间: |
|
| 查看次数: |
6449 次 |
| 最近记录: |