本文介绍了如何检测我是否正在芹菜工人中跑步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法以编程方式确定当前模块的导入/运行是在芹菜工人的背景下完成的?
Is there a way to determine, programatically, that the current module being imported/run is done so in the context of a celery worker?
我们已经解决了在运行Celery worker之前设置环境变量,并在代码中检查此环境变量,但是我想知道是否有更好的方法?
We've settled on setting an environment variable before running the Celery worker, and checking this environment variable in the code, but I wonder if there's a better way?
推荐答案
简单
import sys
IN_CELERY_WORKER_PROCESS = sys.argv and sys.argv[0].endswith('celery')\
and 'worker' in sys.argv
if IN_CELERY_WORKER_PROCESS:
print ('Im in Celery worker')
这篇关于如何检测我是否正在芹菜工人中跑步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!