RuntimeError:切勿在任务Celery中调用result.get() [英] RuntimeError: Never call result.get() within a task Celery
问题描述
我正在使用celery将任务发送到远程服务器,并尝试将结果取回.使用
I am using celery to send a task to remote server and trying to get the result back. The state of task is constantly updated using update_state method on remote server.
我正在使用
app.send_task('task_name')
获取celery任务的结果是一个阻塞的呼叫,我不希望我的django应用程序等待结果和超时.
getting results of celery task is a blocking call and i don't want my django app to wait for result and timeout.
所以我尝试运行另一个芹菜任务以获取结果.
So i tried running another celery task for getting results.
@app.task(ignore_result=True)
def catpure_res(task_id):
task_obj = AsyncResult(task_id)
task_obj.get(on_message=on_msg)
但这会导致以下错误.
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 367, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/celery/app/trace.py", line 622, in __protected_call__
return self.run(*args, **kwargs)
File "/home/arpit/project/appname/tasks/results.py", line 42, in catpure_res
task_obj.get(on_message=on_msg)
File "/usr/local/lib/python2.7/dist-packages/celery/result.py", line 168, in get
assert_will_not_block()
File "/usr/local/lib/python2.7/dist-packages/celery/result.py", line 44, in assert_will_not_block
raise RuntimeError(E_WOULDBLOCK)
RuntimeError: Never call result.get() within a task!
See http://docs.celeryq.org/en/latest/userguide/tasks.html#task-synchronous-subtasks
此错误是否有任何解决方法.我必须运行守护进程来获取结果吗?
Is there any workaround for this error. Do I have to run a daemon process for getting the results?
推荐答案
使用 allow_join_result .请参见下面的代码段.
Use allow_join_result. See the snippet below.
@app.task(ignore_result=True)
def catpure_res(task_id):
task_obj = AsyncResult(task_id)
with allow_join_result():
task_obj.get(on_message=on_msg)
注意:如其他答案所述,它可能会导致性能问题甚至死锁,但是如果您的任务写得好并且不会引起意外错误,那么它应该像一个魅力一样工作.
Note: As mentioned in the other answers it can cause performance issue and even deadlock, but if your task is well written and doesn't cause unexpected errors than it should work like a charm.
这篇关于RuntimeError:切勿在任务Celery中调用result.get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!