2016-07-31 54 views
-1

如果我的所有進程都是從不同的函數啓動的,那麼使用concurrent.futures是沒有問題的。但如果我想用不同的參數調用相同的函數,我似乎無法獲得正確的語法。這是我得到了這麼遠,但它不工作:Python concurrent.futures調用相同的函數兩次

tasks = ((serial_port_local, serial_options_local, serial_port_remote, serial_options_remote, "local"), 
(serial_port_remote, serial_options_remote, serial_port_local, serial_options_local, "remote")) 

for task in zip(tasks, executor.map(serial_cross_over, tasks)): 
    print (task) 

這是錯誤,但我不神交它:

TypeError: serial_cross_over() missing 4 required positional arguments: 'receive_serial_options', 'send_serial_port', 'send_serial_options', and 'source' 

其實我真的不神交爲什麼它的複雜在所有。我不應該只能這樣做:

executor.submit(some_function(parameter1)) 
executor.submit(some_function(parameter2)) 

但這並不奏效。該程序在第二次提交時掛起。爲什麼?

回答

0

似乎serial_cross_over需要4個參數(糾正我,如果我錯了),你不爲他們提供.MAP時左右, 也許看看這個答案:Pass multiple parameters to concurrent.futures.Executor.map?

tasks = ((serial_port_local, serial_options_local, serial_port_remote, serial_options_remote, "local"), (serial_port_remote, serial_options_remote, serial_port_local, serial_options_local, "remote")) 

for task in zip(executor.map(lambda p: f(*p), tasks)): 
    pass 

至於爲什麼executor.submit沒有按預期工作,我不能沒有進一步的細節。你有這樣的嘗試嗎?:

with ThreadPoolExecutor(max_workers=1) as executor: 
    future = executor.submit(some_function, parameter1) 
    print(future.result())