2013-03-05 73 views
6

我需要通過ftp下載文件,將其更改並上傳回去。我使用的芹菜要做到這一點,但我遇到了問題,嘗試使用鏈接的時候,我在哪裏得到:順序連續芹菜鏈接任務

TypeError: upload_ftp_image() takes exactly 5 arguments (6 given)

而且,我可以使用鏈和放心,步驟將是連續的?如果不是什麼替代方案?

res = chain(download_ftp_image.s(server, username , password, "/test_app_2/model.dae" ,"tmp/test_app_2/"), upload_ftp_image.s(server, username , password, "tmp/test_app_2/model.dae" ,"tmp/test_app_2/")).apply_async() 
print res.get() 

任務:

@task() 
def download_ftp_image(ftp_server, username , password , filename, directory): 
    try: 
     ftp = FTP(ftp_server) 
     ftp.login(username, password) 
     if not os.path.exists(directory): 
      os.makedirs(directory) 
      ftp.retrbinary("RETR /default_app/model.dae" , open(directory + 'model.dae', 'wb').write) 
     else: 
      ftp.retrbinary("RETR /default_app/model.dae" , open(directory + 'model.dae', 'wb').write) 
     ftp.quit() 
    except error_perm, resp: 
     raise download_ftp_image.retry(countdown=15) 

    return "SUCCESS: " 

@task() 
def upload_ftp_image(ftp_server, username , password , file , directory): 
    try: 
     ftp = FTP(ftp_server) 
     ftp.login(username, password) 
     new_file= file.replace(directory, "") 
     directory = directory.replace("tmp","") 
     try: 
      ftp.storbinary("STOR " + directory + new_file , open(file, "rb")) 
     except: 
      ftp.mkd(directory) 
      ftp.storbinary("STOR " + directory + new_file, open(file, "rb")) 
     ftp.quit() 
    except error_perm, resp: 
     raise upload_ftp_image.retry(countdown=15) 

    return "SUCCESS: " 

,是這樣的好的或壞的做法對我的具體情況? :

result = download_ftp_image.apply_async((server, username , password, "/test_app_2/model.dae" ,"tmp/test_app_2/",), queue='rep_data') 
result.get() 
result = upload_ftp_image.apply_async((server, username , password, "tmp/test_app_2/model.dae" ,"tmp/test_app_2/",), queue='rep_data') 
#result.get() 

回答

13

A鏈總是通過以前結果作爲第一個參數。從chains documentation

The linked task will be applied with the result of its parent task as the first argument, which in the above case will result in mul(4, 16) since the result is 4.

upload_ftp_image任務不接受這種額外的參數,因此失敗。

你有一個很好的用例鏈接;第二項任務是保證被稱爲第一項任務完成(否則結果無法傳遞)。

只需添加一個參數從以前的任務的結果:

def upload_ftp_image(download_result, ftp_server, username , password , file , directory): 

你可以做一些使用該結果值;也許使下載方法返回下載文件的路徑,以便上傳方法知道要上傳什麼?

+0

我應該怎麼做呢? – psychok7 2013-03-05 13:16:47

+0

@ psychok7:展開一點。 – 2013-03-05 13:20:04

+0

似乎我得到它的工作:) ..謝謝你幫忙 – psychok7 2013-03-05 13:31:45

17

如果您不希望將前一個任務的返回值用作參數,則另一種選擇是使用「不變性」。

http://docs.celeryproject.org/en/latest/userguide/canvas.html#immutability

而不是定義您的子任務爲:

download_ftp_image.s(...) and upload_ftp_image.s(...) 

它們定義爲:

download_ftp_image.si(...) and upload_ftp_image.si(...) 

而且你可以在一個鏈與通常數量的參數現在使用的任務。