Asyncio contains the event_loop.run_in_executor function which allows you to run co-routines in a separate executor - this being a Thread or Processor pool (via the concurrent.futures module):
def blocking_task(i):
print(i) #or some other useful task...
async def run_blocking_tasks(executor):
loop = asyncio.get_event_loop()
blocking_tasks = [
loop.run_in_executor(executor, blocking_task, i)
for i in range(6)
]
completed, pending = await asyncio.wait(blocking_tasks)
results = [t.result() for t in completed]
return results
event_loop = asyncio.get_event_loop()
try:
event_loop.run_until_complete(
run_blocking_tasks(executor)
)
finally:
event_loop.close()
Important: note that the last argument to _run_inexecutor is *args, i.e. the arguments to pass to the blocking task. Passing the argument directly (blocking_task(i)) will not work as expected
Asyncio contains the
event_loop.run_in_executor
function which allows you to run co-routines in a separate executor - this being a Thread or Processor pool (via the concurrent.futures module):Important: note that the last argument to _run_inexecutor is *args, i.e. the arguments to pass to the blocking task. Passing the argument directly (
blocking_task(i)
) will not work as expected