Add thread_pool_patterns

This commit is contained in:
Roland Thomas Jr 2024-05-12 22:40:34 -04:00
parent b25f464a04
commit d528285828
Signed by: roland
GPG Key ID: 7C3C2B085A4C2872
7 changed files with 189 additions and 3 deletions

View File

@ -15,7 +15,8 @@ import time
def blocking_function():
time.sleep(2)
print("Blocking function started!")
time.sleep(5)
return "Blocking function completed!"
@ -29,7 +30,7 @@ async def main():
loop = asyncio.get_event_loop()
executor = ThreadPoolExecutor()
async_task = asyncio.create_task(async_function())
async_task = loop.create_task(async_function())
result = await loop.run_in_executor(executor, blocking_function)
print(result)
@ -38,4 +39,6 @@ async def main():
if __name__ == "__main__":
asyncio.run(main())
loop = asyncio.get_event_loop()
main_task = loop.create_task(main())
loop.run_until_complete(main_task)

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

View File

@ -0,0 +1,22 @@
#!/usr/bin/env python3
from time import sleep
from random import randint
from concurrent.futures import ThreadPoolExecutor, as_completed
def add_one(number):
print(number)
sleep(randint(0,2))
return number + 1
def main():
with ThreadPoolExecutor(32) as executor:
futures = [executor.submit(add_one, number) for number in range(10)]
print(futures)
for future in as_completed(futures):
print(future.result())
if __name__ == "__main__":
main()

View File

@ -0,0 +1,45 @@
#!/usr/bin/env python3
import timeit
from time import sleep
from random import randint
from concurrent.futures import ThreadPoolExecutor, Future
results: list[int] = []
def add_one(number: int) -> int:
sleep(randint(0,2))
result = number + 1
return result
def aggregate_results(future: Future):
results.append(future.result())
def first_method():
with ThreadPoolExecutor(32) as executor:
futures = [executor.submit(add_one, number) for number in range(10)]
for future in futures:
future.add_done_callback(aggregate_results)
def second_method():
futures = []
with ThreadPoolExecutor(32) as executor:
for number in range(10):
futures.append(executor.submit(add_one, number))
futures[-1].add_done_callback(aggregate_results)
def main():
print(timeit.timeit(first_method, number=1))
print(results)
print("All done!\n")
results.clear()
print(timeit.timeit(second_method, number=1))
print(results)
print("All done!")
if __name__ == "__main__":
main()

View File

@ -0,0 +1,22 @@
#!/usr/bin/env python3
from time import sleep
from random import randint
from concurrent.futures import ThreadPoolExecutor
def add_one(number):
print(number)
sleep(randint(0,2))
return number + 1
def main():
with ThreadPoolExecutor(32) as executor:
futures = [executor.submit(add_one, number) for number in range(10)]
print(futures)
for future in futures:
print(future.result())
if __name__ == "__main__":
main()

View File

@ -0,0 +1,22 @@
#!/usr/bin/env python3
from time import sleep
from random import randint
from concurrent.futures import ThreadPoolExecutor, Future, wait, FIRST_COMPLETED
results: list[int] = []
def add_one(number: int) -> int:
sleep(randint(0,2))
return number + 1
def main() -> None:
executor = ThreadPoolExecutor(32)
futures: list[Future] = [executor.submit(add_one, number) for number in range(10)]
done, not_done = wait(futures, return_when=FIRST_COMPLETED)
executor.shutdown(wait=False, cancel_futures=True)
print(done.pop().result())
if __name__ == "__main__":
main()

View File

@ -0,0 +1,39 @@
#!/usr/bin/env python3
from time import sleep
from random import randint
from concurrent.futures import ThreadPoolExecutor, wait, Future
results: list[int] = []
def add_one(number: int) -> int:
print(number)
sleep(randint(0,2))
return number + 1
def main() -> None:
executor = ThreadPoolExecutor(32)
futures: list[Future] = [executor.submit(add_one, number) for number in range(10)]
wait(futures)
print("Done waiting!")
for future in futures:
print(future.result())
print("All done!")
futures: list[Future] = [executor.submit(add_one, number) for number in range(10)]
executor.shutdown()
print("Done waiting!")
for future in futures:
print(future.result())
print("All done!")
with ThreadPoolExecutor() as executor:
futures: list[Future] = [executor.submit(add_one, number) for number in range(10)]
print("Done waiting!")
for future in futures:
print(future.result())
print("All done!")
if __name__ == "__main__":
main()