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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With