I'm working on migrating a Python 3 codebase from complete sync to partially asyncio.
The reason it is partial is because the part of the service that read messages is not compatible with asyncio, yet so it has to remain something like this:
for message in read_messages():
handle_message(message)
I'm converting handle_message
to handle_message_async
which is defined as async function and everything inside of it will be asyncio compatible. For now I want to continue handle messages one by one and be able to use asyncio inside the handling of a single message.
My question is what is the difference between those two options:
asyncio.run
for message in read_messages():
asyncio.run(handle_message_async(message))
asyncio.Runner()
with asyncio.Runner() as async_runner:
for message in read_messages():
async_runner.run(handle_message_async(message))
Is there a difference in term of setup and teardown that maybe happening in asyncio.run
?
Is there a difference in how exceptions will be raised back to the sync part of the code?
I'm working on migrating a Python 3 codebase from complete sync to partially asyncio.
The reason it is partial is because the part of the service that read messages is not compatible with asyncio, yet so it has to remain something like this:
for message in read_messages():
handle_message(message)
I'm converting handle_message
to handle_message_async
which is defined as async function and everything inside of it will be asyncio compatible. For now I want to continue handle messages one by one and be able to use asyncio inside the handling of a single message.
My question is what is the difference between those two options:
asyncio.run
for message in read_messages():
asyncio.run(handle_message_async(message))
asyncio.Runner()
with asyncio.Runner() as async_runner:
for message in read_messages():
async_runner.run(handle_message_async(message))
Is there a difference in term of setup and teardown that maybe happening in asyncio.run
?
Is there a difference in how exceptions will be raised back to the sync part of the code?
Share Improve this question asked Mar 28 at 10:38 Ido RanIdo Ran 11.4k22 gold badges93 silver badges156 bronze badges1 Answer
Reset to default 2When a call to asyncio.run
is called a new event loop is created as well as a new execution context. On return the event loop is closed and the context effectively destroyed. A successive call to asyncio.run
will consequently need to create a new event loop and a new context. By using asyncio.Runner
you will be creating an event loop and context once and reusing them for every call to handle_message_async
in your with
block.
Reusing the same event loop is basically a performance optimization. But reusing the same context provides more than a performance gain: Reusing the context will be necessary if you are creating any context variables that you need to persist across the multiple calls to handle_message_async
.
As far as how exceptions are raised back to the caller, I haven't seen any difference in using the two different approaches.