I would like to submit jobs from a thread to an asyncio
event loop (just like run_in_executor but the other way around).
Here's what the asyncio
documentation says about concurrency and multithreading:
To schedule a callback from a different thread, the BaseEventLoop.call_soon_threadsafe() method should be used. Example to schedule a coroutine from a different thread:
loop.call_soon_threadsafe(asyncio.async, coro_func())
That works fine but the result of the coroutine is lost.
Instead, it is possible to use a function that adds a done callback to the future returned by async
(or ensure_future
) so that the thread can access the result through a concurrent.futures.Future.
Is there a particular reason why such a feature is not implemented in the standard library? Or did I miss a simpler way to achieve that?