Add thread_pool_patterns
This commit is contained in:
33
thread_pool_patterns/map_and_wait.py
Executable file
33
thread_pool_patterns/map_and_wait.py
Executable file
@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env python3
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from random import randint
|
||||
from time import sleep
|
||||
|
||||
|
||||
def add_one(number):
|
||||
sleep(randint(0, 2))
|
||||
return number + 1
|
||||
|
||||
|
||||
def add_one_print(number):
|
||||
sleep(randint(0, 2))
|
||||
print(number)
|
||||
return number + 1
|
||||
|
||||
|
||||
def main():
|
||||
with ThreadPoolExecutor() as executor:
|
||||
for result in executor.map(add_one, [1, 2, 3, 4, 5]):
|
||||
print(result)
|
||||
print("All done!\n")
|
||||
|
||||
with ThreadPoolExecutor() as executor:
|
||||
returned_generator = executor.map(add_one_print, [1, 2, 3, 4, 5])
|
||||
print("Results in order\n")
|
||||
for result in returned_generator:
|
||||
print(result)
|
||||
print("All done!")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user