admin管理员组文章数量:1356219
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.
本文标签: Migrating Python sync code to asynciodifference between asynciorun vs asyncioRunnerStack Overflow
版权声明:本文标题:Migrating Python sync code to asyncio - difference between asyncio.run vs asyncio.Runner - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744042512a2580924.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论