dvas0004 / NerdNotes

A collection of notes: things I'd like to remember while reading technical articles, technical questions I couldn't answer, and so on.
11 stars 0 forks source link

Python: co-routines in multi-threading/processing #120

Open dvas0004 opened 4 years ago

dvas0004 commented 4 years ago

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

dvas0004 commented 4 years ago

https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor https://pymotw.com/3/asyncio/executors.html