classification
Title: AbstactEventLoop.run_in_executor is listed as an async method, but should actually return a Futrue
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8, Python 3.7, Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: aeros, asvetlov, jamesba, yselivanov
Priority: normal Keywords:

Created on 2020-05-26 15:45 by jamesba, last changed 2020-07-01 08:03 by jamesba.

Messages (3)
msg370005 - (view) Author: James Barrett (jamesba) Date: 2020-05-26 15:45
As discussed in < https://github.com/python/typeshed/issues/3999#issuecomment-634097968 > the type of `AbstractEventLoop.run_in_executor` is defined at < https://github.com/python/cpython/blob/master/Lib/asyncio/events.py#L286 > as follows:

```
async def run_in_executor(self, executor, func, *args):
        raise NotImplementedError
```

However all concrete implementations of this method are actually not async methods but rather synchronous methods which return a Future object.

Logically this appears to make sense: at base `run_in_executor` is not a coroutine, since it doesn't create an object representing code which will be executed when the object is awaited, rather it returns an object representing code which is running asynchronously elsewhere (on another thread) and which can be awaited to wait for that other thread to complete its task. Which seems to be a perfect match to what a Future object is supposed to be.

As such it seems that the current definition of the method as a coroutine is possibly a mistake.

Alternatively if some feel that it is important to allow concrete implementations to implement it as a coroutine if they need to then perhaps it could be specified to be a method returning an Awaitable, since that would cover both options?
msg370045 - (view) Author: Kyle Stanley (aeros) * (Python committer) Date: 2020-05-27 03:37
From looking at the commit history of AbstactEventLoop.run_in_executor(), it seems that it was previously be a non-coroutine method prior to the conversion from the `@asyncio.coroutine` decorator to `async def` (PR-4753). See https://github.com/python/cpython/blame/ede157331b4f9e550334900b3b4de1c8590688de/Lib/asyncio/events.py#L305.

The only context for the change I can find is the following conversation between Andrew and Yury: https://github.com/python/cpython/pull/4753#issuecomment-350114336. However, the example provided of `connect_read_pipe()` had already been a coroutine at the time for the BaseEventLoop implementation, which makes sense in that case. So, it's not clear to me as to why `run_in_executor()` was also converted to "async def" when its main implementation is not a coroutine. Furthermore, it's documented as an awaitable rather than a coroutine (https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor).

@Andrew do you have any additional context to provide that I'm potentially missing?
msg372735 - (view) Author: James Barrett (jamesba) Date: 2020-07-01 08:03
Is there any further movement on this?
History
Date User Action Args
2020-07-01 08:03:53jamesbasetmessages: + msg372735
2020-05-27 03:37:08aerossetnosy: + asvetlov, yselivanov, aeros
messages: + msg370045
2020-05-26 15:45:38jamesbacreate