无法获得实时信息的芹菜任务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法获得实时信息的芹菜任务相关的知识,希望对你有一定的参考价值。
我想获取芹菜信息的任务,然后参考文档[http://docs.celeryproject.org/en/latest/userguide/workers.html?highlight=revoke#inspecting-workers)这是代码。
celery.py
app = Celery('tasks', broker='redis://127.0.0.1:6379/0')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
i = app.control.inspect()
views.py
from myproject.celery import i
class AAA(APIView):
def get(self,request):
print(i.registered())
print(i.active())
print(i.scheduled())
print(i.reserved())
return Response("ok")
这是打印结果
{'celery@wwl': ['quantum.tasks.pro_execute']}
{'celery@wwl': []}
{'celery@wwl': []}
{'celery@wwl': []}
task.py
@task
def pro_execute(code, oper_id, number_of_shots):
operation = Operation.objects.filter(id=oper_id, status=0).first()
if not operation:
return
operation.start_date = datetime.now()
operation.status = 1
operation.save()
try:
qc = QuantumCircuit.from_qasm_str(code)
backend = Aer.get_backend('qasm_simulator')
job_sim = execute(qc, backend, shots=number_of_shots)
sim_result = job_sim.result()
ope_result = sim_result.get_counts(qc)
operation.result = ope_result
operation.end_date = datetime.now()
operation.status = 2
except Exception as e:
logging.error("execute program failed:error %s" % str(e))
operation.result = str(e)
operation.status = 3
operation.save()
无法获得最后三个结果?我不知道为什么但是可以在鲜花仪表板上看到它们非常感谢。
答案
在celery.py中添加后端
app = Celery('tasks', broker='redis://127.0.0.1:6379/0', backend='db+sqlite:///results.sqlite')
现在,您可以查看任务是否成功完成或失败。如果出现错误,它将记录在数据库中。
以上是关于无法获得实时信息的芹菜任务的主要内容,如果未能解决你的问题,请参考以下文章