Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ThreadPoolExecutor with all params from two lists

I have two lists:

a = [1, 2, 3, 4]
b = [9, 8, 7, 6]

I'd like to have every combination of these two lists passed as argument to a function I'm multithreading:

def test(hello, world):
    return hello + world

with ThreadPoolExecutor(max_workers=10) as executor:
    future_to_stuff = { executor.submit(self._http_check_port, hello, world): ### }
    for future in as_completed(future_to_port):
        ...

I'm trying to figure out how to "unpack" both my lists so that every combination of values in a and b are sent as params to the function.

like image 887
Jimmy Sanchez Avatar asked Jan 14 '18 01:01

Jimmy Sanchez


1 Answers

I usually use following list comprehension.

future_to_stuff = [executor.submit(test, hello, world) 
                   for hello, world in zip(a, b)]

Here is a modified code.

from concurrent.futures import ThreadPoolExecutor, as_completed

def test(hello, world):
    return hello + world

def main(a, b):
    with ThreadPoolExecutor(max_workers=10) as executor:
        future_to_stuff = [executor.submit(test, hello, world) 
                           for hello, world in zip(a, b)]
        for future in as_completed(future_to_stuff):
            print(future.result())

if __name__ == '__main__':
    a = [1, 2, 3, 4]
    b = [9, 8, 7, 6]
    main(a, b)

Another way is to use .map method instead of .submit.

from concurrent.futures import ThreadPoolExecutor, as_completed

def test(hello, world):
    return hello + world

def main(a, b):
    with ThreadPoolExecutor(max_workers=10) as executor:
        results = executor.map(test, a, b)
        for result in results:
            print(result)

if __name__ == '__main__':
    a = [1, 2, 3, 4]
    b = [9, 8, 7, 6]
    main(a, b)
like image 103
dkato Avatar answered Oct 31 '22 14:10

dkato