bpo-36373: Fix deprecation warnings by asvetlov · Pull Request #15889 · python/cpython
I ended up here after following deprecation warning -> git blame -> ticket -> comments.
So reviving it as may be useful for others following the path.
I have a case of an asynchronous loop running on another thread already, and a synchronous program that creates tasks to run on it. Basically like background workers.
Trying to go a generic way here. Maybe they would be some existing helpers but I did not find it yet.
async def as_coroutine(cb, *args, **kwargs):
return cb(*args, **kwargs)
def call_on_loop(cb, *args, loop, **kwargs):
"""Run a callback on the async loop and return its return value."""
coro = as_coroutine(cb, *args, **kwargs)
return asyncio.run_coroutine_threadsafe(coro, loop).result()
# Loop is running already somewhere
queue = call_on_loop(asyncio.Queue, loop=loop)