答案 0 :(得分:18)
您可以通过导入celery status
包
celery.bin.celery
命令
import celery
import celery.bin.base
import celery.bin.celery
import celery.platforms
app = celery.Celery('tasks', broker='redis://')
status = celery.bin.celery.CeleryCommand.commands['status']()
status.app = status.get_app()
def celery_is_up():
try:
status.run()
return True
except celery.bin.base.Error as e:
if e.status == celery.platforms.EX_UNAVAILABLE:
return False
raise e
if __name__ == '__main__':
if celery_is_up():
print('Celery up!')
else:
print('Celery not responding...')
答案 1 :(得分:2)
如何使用子进程,不确定它是否是一个好主意:
>>> import subprocess
>>> output = subprocess.check_output('ps aux'.split())
>>> 'supervisord' in output
True
答案 2 :(得分:2)
您可以从supervisorctl status
输出
import subprocess
def is_celery_worker_running():
ctl_output = subprocess.check_output('supervisorctl status celery_worker'.split()).strip()
if ctl_output == 'unix:///var/run/supervisor.sock no such file':
# supervisord not running
return False
elif ctl_output == 'No such process celery_worker':
return False
else:
state = ctl_output.split()[1]
return state == 'RUNNING'
答案 3 :(得分:1)
A sparse web user interface comes with supervisor. May be you could use that. It can be enabled in the supervisor config. Key to look for is [inet_http_server]
You could even look at the source code of that piece to get ideas to implement your own.
答案 4 :(得分:0)
答案 5 :(得分:0)
受@vgel's answer启发,使用Celery 4.3.0。
def user_task_entry(request):
title = 'Assign Task'
form = TaskForm(request.POST or None)
if form.is_valid():
instance = form.save(commit=False)
instance.assigned_by = request.user
instance.save()
return redirect('taskmis:user_task_list')
context = {
"title": title,
"form": form,
}
return render(request, "task_entry.html",context)
答案 6 :(得分:-2)
根据我的经验,我会设置一条消息来跟踪它是否完整,以便队列负责重试任务。