| import time
|
| import functools
|
| import concurrent.futures
|
| import asyncio
|
|
|
|
|
| def slow_task(i):
|
| print(f'running task: {i}')
|
| time.sleep(i)
|
| return i
|
|
|
|
|
|
|
| async def main():
|
| loop = asyncio.get_event_loop()
|
| aws = []
|
| with concurrent.futures.ThreadPoolExecutor() as executor:
|
| for i in range(5):
|
| aws.append(loop.run_in_executor(executor, functools.partial(slow_task, i)))
|
|
|
| res = await asyncio.gather(*aws)
|
| print(res)
|
|
|
|
|
|
|
| asyncio.run(main())
|