How to use concurrent.futures in Python

我的未来我决定 提交于 2021-01-28 08:44:18

问题


Im struggling to get multithreading working in Python. I have i function which i want to execute on 5 threads based on a parameter. I also needs 2 parameters that are the same for every thread. This is what i have:

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]

But i don't know what to do next


回答1:


If you don't care about the order, you can now do:

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())

Otherwise, if you care about order, it might make sense to use ThreadPoolExecutor.map with functools.partial to fill in the arguments that are always the same:

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
    ))


来源:https://stackoverflow.com/questions/49358473/how-to-use-concurrent-futures-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!