Migrating Python sync code to asyncio - difference between asyncio.run vs asyncio.Runner - Stack Overflow

I'm working on migrating a Python 3 codebase from complete sync to partially asyncio.The reason i

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:

  1. asyncio.run
for message in read_messages():
  asyncio.run(handle_message_async(message))
  1. 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:

  1. asyncio.run
for message in read_messages():
  asyncio.run(handle_message_async(message))
  1. 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 badges
Add a comment  | 

1 Answer 1

Reset to default 2

When 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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744042512a4548574.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信