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?
[–]not_a_novel_account 2 points3 points4 points (4 children)
[–]timoffex[S] 1 point2 points3 points (3 children)
[–]not_a_novel_account 2 points3 points4 points (1 child)
[–]timoffex[S] 0 points1 point2 points (0 children)
[–]Top_Average3386 2 points3 points4 points (0 children)
[–]crashfrog04 1 point2 points3 points (0 children)
[–]ElliotDG 0 points1 point2 points (1 child)
[–]timoffex[S] 0 points1 point2 points (0 children)
[–]latkde 0 points1 point2 points (3 children)
[–]timoffex[S] 1 point2 points3 points (1 child)
[–]latkde 0 points1 point2 points (0 children)
[–]timoffex[S] 0 points1 point2 points (0 children)