a simple example of a Python coroutine:
async def greet(name):
return f"Hello, {name}!"
async def main():
result = await greet("Alice")
print(result)
# Run the coroutine
import asyncio
asyncio.run(main())
an example of using a thread pool
import concurrent.futures
def greet(name):
return f"Hello, {name}!"
def main():
with concurrent.futures.ThreadPoolExecutor() as executor:
future = executor.submit(greet, "Alice")
print(future.result())
# Run the function
main()
an example of submitting tasks into a thread pool
import concurrent.futures
def greet(name):
return f"Hello, {name}!"
# Create a ThreadPoolExecutor
executor = concurrent.futures.ThreadPoolExecutor()
def main():
names = ["Alice", "Bob", "Charlie"]
# Submit tasks into the thread pool
futures = [executor.submit(greet, name) for name in names]
# Retrieve results as they become available
for future in concurrent.futures.as_completed(futures):
print(future.result())
# Run the function
main()