从celery任务中调用async_result.get() [英] Calling async_result.get() from within a celery task

查看:111
本文介绍了从celery任务中调用async_result.get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个celery任务,它调用另一个 remote 任务(该任务在另一台celery应用程序的另一台服务器上.).当我尝试像这样从我的任务中获取远程任务的结果时:

I have a celery task that calls another remote task (it's on a different celery app, in another server..). When I try to .get() the result of that remote task from within my task like this:

@app.task()
def my_local_task():
  result_from_remote = app.send_task('remote_task', [arg1, arg2])
  return result_from_remote.get()

我收到此错误:

RuntimeWarning: Never call result.get() within a task! See http://docs.celeryq.org/en/latest/userguide/tasks.html#task-synchronous-subtasks

In Celery 3.2 this will result in an exception being
raised instead of just being a warning.

  warnings.warn(RuntimeWarning(E_WOULDBLOCK))

基本上,我希望我的任务是同步的"-我希望它等待远程任务的结果,我可以接受.

Basically I want my task to be "synchronous" - I want it to wait for the result of the remote task, and i'm ok with it.

我可以告诉芹菜吗?当然有链的解决方案,除了不可能链远程任务.调用远程任务的唯一方法是使用app.send_task,它返回一个AsyncResult,并且由于需要任务功能本身,所以无法链接.

Can I tell celery it's ok? There is the solution of chaining of course, except it's impossible to chain remote tasks. The only way to call remote tasks is using app.send_task, which returns an AsyncResult, and that I can't chain as I need the task function itself..

推荐答案

如果您希望您的任务是同步的,则可以使用 ready()来保持循环:

If you want your task to be synchronous, you can use ready() to hold a loop:

import time

while not result_from_remote.ready():
    time.sleep(5)

return result_from_remote.get()

这篇关于从celery任务中调用async_result.get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆