2013-01-18 80 views
7

芹菜文檔告訴我,如果多個任務鏈接在一起,第一個任務的結果將是下一個任務的第一個參數。我的問題是,當我有一個返回多個結果的任務時,我無法讓它工作。芹菜:多重參數鏈接任務

例:

@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 

並得到兩個結果。

+0

想要解決「返回下一個功能的位置參數」問題的用戶可能會對我的答案感興趣http://stackoverflow.com/a/15778196/114917 –

回答

20

這裏有兩個錯誤。

首先,您不必撥打get_comments()render_template()。相反,您應該使用.s()任務方法。像:

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

在你的情況,你首先啓動函數,然後嘗試將函數結果連接到一個鏈。

其次,實際上,您不會從第一個任務中返回「兩個結果」。相反,您返回一個包含兩個結果的元組,並將此元組作爲單個對象傳遞給第二個任務。

因此,你應該重寫你的第二個任務,

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

如果你解決這些,它應該工作。

+0

這個固定的礦井也是如此。 – ashim888

+0

這是唯一的方法嗎?有沒有可能通過星級爭論來實現它? – FavorMylikes