Python+Celery:链接作业?
Celery 文档表明这是一个让任务等待其他任务的结果是个坏主意……但是建议的解决方案(参见“好”标题)还有一些不足之处。具体来说,没有明确的方法将子任务的结果返回给调用者(而且,这有点难看)。
那么,有没有什么方法可以“链接”作业,以便调用者获得最终作业的结果呢?例如,使用 add
示例:
>>> add3 = add.subtask(args=(3, ))
>>> add.delay(1, 2, callback=add3).get()
6
或者,可以返回 Result 的实例吗?例如:
@task
def add(x, y, callback=None):
result = x + y
if callback:
return subtask(callback).delay(result)
return result
这将使链中“最终”作业的结果可以通过简单的操作来检索:
result = add(1, 2, callback=add3).delay()
while isinstance(result, Result):
result = result.get()
print "result:", result
The Celery documentation suggests that it's a bad idea to have tasks wait on the results of other tasks… But the suggested solution (see “good” heading) leaves a something to be desired. Specifically, there's no clear way of getting the subtask's result back to the caller (also, it's kind of ugly).
So, is there any way of “chaining” jobs, so the caller gets the result of the final job? Eg, to use the add
example:
>>> add3 = add.subtask(args=(3, ))
>>> add.delay(1, 2, callback=add3).get()
6
Alternately, is it OK to return instances of Result? For example:
@task
def add(x, y, callback=None):
result = x + y
if callback:
return subtask(callback).delay(result)
return result
This would let the result of the “final” job in the chain could be retrived with a simple:
result = add(1, 2, callback=add3).delay()
while isinstance(result, Result):
result = result.get()
print "result:", result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以用芹菜链来做。请参阅https://celery.readthedocs.org/en/latest/userguide/canvas.html#chains
链接作业:
You can do it with a celery chain. See https://celery.readthedocs.org/en/latest/userguide/canvas.html#chains
Chaining job: