9

セロリのドキュメントによると、複数のタスクがチェーンされている場合、最初のタスクの結果が次の最初の引数になることがわかります。私の問題は、複数の結果を返すタスクがある場合、それを機能させることができないことです。

例:

@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

ここで、それらを (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

両方の結果を取得します。

4

1 に答える 1

26

ここには2つの間違いがあります。

get_comments()まず、 andを呼び出す必要はありませんrender_template()。代わりに、.s()タスク メソッドを使用する必要があります。お気に入り:

( get_comments.s(url) | render_template.s()).apply_async()

あなたの場合、最初に関数を起動してから、関数の結果をチェーンに結合しようとします。

第二に、実際には、最初のタスクから「2 つの結果」を返すことはありません。代わりに、両方の結果を含むタプルを返します。このタプルは、単一のオブジェクトとして 2 番目のタスクに渡されます。

したがって、2 番目のタスクを次のように書き直す必要があります。

@task
def render_template(comments_and_submission):
   comments, submission = comments_and_submission

これらを修正すれば、うまくいくはずです。

于 2013-01-21T15:29:06.990 に答える