如何在Python中使用concurrent.futures

4

我在Python中想要实现多线程,但是遇到了一些困难。我有一个函数需要基于一个参数在5个线程上执行,同时还需要两个对于每个线程都相同的参数。这是我的代码:

from concurrent.futures import ThreadPoolExecutor

def do_something_parallel(sameValue1, sameValue2, differentValue):
    print(str(sameValue1))        #same everytime
    print(str(sameValue2))        #same everytime
    print(str(differentValue))    #different

main(): 

    differentValues = ["1000ms", "100ms", "10ms", "20ms", "50ms"]

    with ThreadPoolExecutor(max_workers=5) as executor:
        futures = [executor.submit(do_something_parallel, sameValue1, sameValue2, differentValue) for differentValue in differentValues]

但我不知道接下来该怎么做

1个回答

6

如果您不关心顺序,现在可以执行以下操作:

from concurrent.futures import as_completed

# The rest of your code here

for f in as_completed(futures):
    # Do what you want with f.result(), for example:
    print(f.result())

否则,如果您关心顺序,使用ThreadPoolExecutor.mapfunctools.partial填充始终相同的参数可能是有意义的。
from functools import partial

# The rest of your code...

with ThreadPoolExecutor(max_workers=5) as executor:
    results = executor.map(
        partial(do_something_parallel, sameValue1, sameValue2),
        differentValues
    ))

如何使用关键字参数来完成这个任务?尝试使用executor.map(partial(fun, param1=arg1, param3=arg3), param2=arg2)会抛出错误,而删除param2=又会导致错误,因为可迭代参数被传递给了第一个参数。 - Buzz B

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接