`asyncio` as a library implementation detail? /u/timoffex Python Education

Hi all,

I maintain a library with a synchronous interface, i.e. no async def functions. A few of our functions are blocking but do work asynchronously using thread-based parallelism. Part of the code is essentially an ad-hoc event loop that invokes callbacks.

The callback-based approach is not very readable, and it lacks some flexibility that’s becoming important, so I want to rewrite this using coroutines.

To avoid adding new dependencies, I considered asyncio, but asyncio.run() documents that it cannot be called if an event loop is already running. asyncio.set_event_loop() and asyncio.get_event_loop() look promising

orig_loop = asyncio.get_event_loop() asyncio.set_event_loop(None) # Would this work? try: asyncio.run(_impl()) finally: asyncio.set_event_loop(orig_loop) 

But I can’t tell if this would work in all cases based on the documentation.

Is it possible to introduce asyncio to a non-async library function without it being a breaking change? Or should I write a simple event loop with just the functionality I need?

submitted by /u/timoffex
[link] [comments]

​r/learnpython Hi all, I maintain a library with a synchronous interface, i.e. no async def functions. A few of our functions are blocking but do work asynchronously using thread-based parallelism. Part of the code is essentially an ad-hoc event loop that invokes callbacks. The callback-based approach is not very readable, and it lacks some flexibility that’s becoming important, so I want to rewrite this using coroutines. To avoid adding new dependencies, I considered asyncio, but asyncio.run() documents that it cannot be called if an event loop is already running. asyncio.set_event_loop() and asyncio.get_event_loop() look promising orig_loop = asyncio.get_event_loop() asyncio.set_event_loop(None) # Would this work? try: asyncio.run(_impl()) finally: asyncio.set_event_loop(orig_loop) But I can’t tell if this would work in all cases based on the documentation. Is it possible to introduce asyncio to a non-async library function without it being a breaking change? Or should I write a simple event loop with just the functionality I need? submitted by /u/timoffex [link] [comments] 

Hi all,

I maintain a library with a synchronous interface, i.e. no async def functions. A few of our functions are blocking but do work asynchronously using thread-based parallelism. Part of the code is essentially an ad-hoc event loop that invokes callbacks.

The callback-based approach is not very readable, and it lacks some flexibility that’s becoming important, so I want to rewrite this using coroutines.

To avoid adding new dependencies, I considered asyncio, but asyncio.run() documents that it cannot be called if an event loop is already running. asyncio.set_event_loop() and asyncio.get_event_loop() look promising

orig_loop = asyncio.get_event_loop() asyncio.set_event_loop(None) # Would this work? try: asyncio.run(_impl()) finally: asyncio.set_event_loop(orig_loop) 

But I can’t tell if this would work in all cases based on the documentation.

Is it possible to introduce asyncio to a non-async library function without it being a breaking change? Or should I write a simple event loop with just the functionality I need?

submitted by /u/timoffex
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *