python - 任务 Celery 中的 RuntimeError : Never call result. get()

标签 python django redis celery

我正在使用 celery 将任务发送到远程服务器并尝试取回结果。使用 update_state 不断更新任务状态远程服务器上的方法。

我正在使用

发送任务
app.send_task('task_name')

获取 celery 任务的结果是一个阻塞调用,我不希望我的 Django 应用程序等待结果和超时。

所以我尝试运行另一个 celery 任务来获得结果。

@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

是否有任何解决此错误的方法。我是否必须运行守护进程才能获得结果?

最佳答案

使用allow_join_result .请参阅下面的代码段。

@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)

注意:如其他答案中所述,它可能会导致性能问题甚至死锁,但如果您的任务写得很好并且不会导致意外错误,那么它应该会很有魅力。

关于python - 任务 Celery 中的 RuntimeError : Never call result. get(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45490722/

相关文章:

python - 随机 Python 模拟的校准

python - 如何从 C 代码访问 python bool 变量?

python - 如何使用 Python 将多列数据合并到各自的行中

python - Redis 或 Memcached 是否可以用于 "lock"资源,例如 S3 之类的 blobstore?

python - 如何迭代 Django 模板中的列表?

python - ElementTree 实例没有属性 'fromstring' 。那么,我做错了什么?

django - django开发服务器运行时,如何 curl 127.0.0.1/8000?

caching - 使用 Redis 和 Service Stack for Piranha 编写缓存提供程序 - 跟踪缓存的对象类型

django - 缩放 Gevent Socketio

字符串上的 Python hash() 函数