我需要的是一个简单的黑客同步运行功能,如果芹菜不活跃。你知道吗
我尝试的是:
is_celery_working
返回False,尽管芹菜和Redis都在运行(分别是rancelery -A project worker -l debug
和redis-server
)。而且get_celery_worker_status
总是给error
状态。你知道吗
我在用芹菜配姜果。你知道吗
from project.celery import app
def is_celery_working():
result = app.control.broadcast('ping', reply=True, limit=1)
return bool(result) # True if at least one result
def sync_async(func):
if is_celery_working():
return func.delay
else:
return func
sync_async(some_func)(**its_args, **its_kwrgs)
def get_celery_worker_status():
error_key = 'error'
try:
from celery.task.control import inspect
insp = inspect()
d = insp.stats()
if not d:
d = {error_key: 'No running Celery workers were found.'}
except IOError as e:
from errno import errorcode
msg = "Error connecting to the backend: " + str(e)
if len(e.args) > 0 and errorcode.get(e.args[0]) == 'ECONNREFUSED':
msg += ' Check that the RabbitMQ server is running.'
d = {error_key: msg}
except ImportError as e:
d = {error_key: str(e)}
return d
def sync_async(func):
status = get_celery_worker_status()
if 'error' not in status:
return func.delay
else:
return func
sync_async(some_func)(**its_args, **its_kwrgs)
简单的
is_celery_working
函数看起来是正确的。如果得到False
,则可能需要使用可选的timeout
参数将超时时间增加到5
或10
秒。你知道吗如果Redis服务器不工作,这只会给出一个错误。这对我来说很好,因为我假设如果Redis不工作,那么芹菜就停止了。你知道吗
相关问题 更多 >
编程相关推荐