2016-07-31 53 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())