Add thread_pool_patterns
This commit is contained in:
44
thread_pool_patterns/asyncio_with_thread_pool.py
Executable file
44
thread_pool_patterns/asyncio_with_thread_pool.py
Executable file
@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
"""thread_pool.py
|
||||
----------------
|
||||
This module demonstrates the use of asyncio to handle asynchronous and blocking
|
||||
functions in Python. The module contains three functions: a blocking function
|
||||
that simulates a long-running process with `time.sleep`, an asynchronous
|
||||
function that demonstrates asyncio's sleep, and a main function that runs
|
||||
both the blocking and async functions concurrently using a ThreadPoolExecutor
|
||||
for the blocking function.
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
import time
|
||||
|
||||
|
||||
def blocking_function():
|
||||
print("Blocking function started!")
|
||||
time.sleep(5)
|
||||
return "Blocking function completed!"
|
||||
|
||||
|
||||
async def async_function():
|
||||
print("Async function started!")
|
||||
await asyncio.sleep(1)
|
||||
print("Async function completed!")
|
||||
|
||||
|
||||
async def main():
|
||||
loop = asyncio.get_event_loop()
|
||||
executor = ThreadPoolExecutor()
|
||||
|
||||
async_task = loop.create_task(async_function())
|
||||
|
||||
result = await loop.run_in_executor(executor, blocking_function)
|
||||
print(result)
|
||||
|
||||
await async_task
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
loop = asyncio.get_event_loop()
|
||||
main_task = loop.create_task(main())
|
||||
loop.run_until_complete(main_task)
|
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()
|
22
thread_pool_patterns/submit_use_as_completed.py
Executable file
22
thread_pool_patterns/submit_use_as_completed.py
Executable 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()
|
45
thread_pool_patterns/submit_use_callback.py
Executable file
45
thread_pool_patterns/submit_use_callback.py
Executable 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()
|
22
thread_pool_patterns/submit_use_sequentially.py
Executable file
22
thread_pool_patterns/submit_use_sequentially.py
Executable 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()
|
22
thread_pool_patterns/submit_wait_first.py
Executable file
22
thread_pool_patterns/submit_wait_first.py
Executable 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()
|
39
thread_pool_patterns/submit_wait_for_all.py
Executable file
39
thread_pool_patterns/submit_wait_for_all.py
Executable 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()
|
Reference in New Issue
Block a user