I have a server application and when requested by the client I schedule some work, like
def work():
time.sleep(5)
fut = asyncio.get_event_loop().run_in_executor(None, work)
I await fut later when it is requested explicitly. My use case requires that run_in_executor submit the work function immediately, and that behaves as expected in my environment (Ubuntu 16.04, Python 3.7.1).
Since my application depends on this behavior I wanted to verify that it is not something likely to change, so I checked several resources:
- The documentation seems kind of vague. awaitable seems like it may apply to the method or the return value - though the body of the text does say it returns an
asyncio.Futureexplicitly. - PEP 3156 that specifies asyncio - here it says nothing close to
run_in_executorbeing a coroutine. - In a few issues whether
run_in_executoris a function that returns an awaitable or a coroutine itself seems to be considered an implementation detail. See 25675 and 32327. AbstractEventLoop.run_in_executoris specified as a coroutine, but the implementation inBaseEventLoop.run_in_executoris a plain function.
1 and 2 mostly seem to indicate that the current behavior is correct, but 3 and 4 are concerning. This seems like a very important part of the interface because if the function itself is a coroutine then it will not begin executing (therefore will not schedule the work) until it is awaited.
Is it safe to rely on the current behavior? If so, is it reasonable to change the interface of AbstractEventLoop.run_in_executor to a plain function instead of a coroutine?