Skip to content Skip to sidebar Skip to footer

Submit A Job To An Asyncio Event Loop

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 concurrenc

Solution 1:

My request made its way and a run_coroutine_threadsafe function has been implemented here.

Example:

def target(loop, timeout=None):
    future = asyncio.run_coroutine_threadsafe(add(1, b=2), loop)
    return future.result(timeout)

async def add(a, b):
    await asyncio.sleep(1)
    return a + b

loop = asyncio.get_event_loop()
future = loop.run_in_executor(None, target, loop)
assert loop.run_until_complete(future) == 3

I originally posted a sub-class of concurrent.futures.Executor that can still be implemented as:

classLoopExecutor(concurrent.futures.Executor):
    """An Executor subclass that uses an event loop 
    to execute calls asynchronously."""def__init__(self, loop=None):
        """Initialize the executor with a given loop."""
        self.loop = loop or asyncio.get_event_loop()

    defsubmit(self, fn, *args, **kwargs):
        """Schedule the callable, fn, to be executed as fn(*args **kwargs).
        Return a Future object representing the execution of the callable."""
        coro = asyncio.coroutine(fn)(*args, **kwargs)
        return asyncio.run_coroutine_threadsafe(coro, self.loop)

Post a Comment for "Submit A Job To An Asyncio Event Loop"