celery文档告诉我,如果多个任务链接在一起,第一个任务的结果将是下一个任务的第一个参数.我的问题是,当我有一个返回多个结果的任务时,我无法让它工作.
例:
@task()
def get_comments(url):
#get the comments and the submission and return them as 2 objects
return comments, submission
@task
def render_template(threadComments, submission):
#render the objects into a html file
#does not return anything
Run Code Online (Sandbox Code Playgroud)
现在,如果我在链接中调用它们(get_comments(url)| render_template()).apply_asnc()python将抛出一个TypeError: render_template() takes exactly 2 arguments (0 given)
.
我可以看到结果没有打开并应用于参数.如果我只调用get_comments,我可以这样做:
result = get_comments(url)
arg1, arg2 = result
Run Code Online (Sandbox Code Playgroud)
并得到两个结果.
Rom*_*lov 23
这里有两个错误.
首先,你不必打电话get_comments()
和render_template()
.相反,您应该使用.s()
任务方法.喜欢:
( get_comments.s(url) | render_template.s()).apply_async()
Run Code Online (Sandbox Code Playgroud)
在您的情况下,首先启动该函数,然后尝试将函数结果连接到链.
其次,实际上,你不会从第一个任务中返回"两个结果".相反,您返回一个包含两个结果的元组,并将此元组作为单个对象传递给第二个任务.
因此,您应该将第二个任务重写为
@task
def render_template(comments_and_submission):
comments, submission = comments_and_submission
Run Code Online (Sandbox Code Playgroud)
如果你修复这些,它应该工作.