concurrent futures, async futures and await

Ian Kelly ian.g.kelly at gmail.com
Thu Feb 23 02:38:30 EST 2017


On Wed, Feb 22, 2017 at 12:50 PM, Nagy László Zsolt
<gandalf at shopzeus.com> wrote:
>
> I'm in a situation where I would like to refactor some code to use
> native coroutine functions (aync def) instead of "normal" coroutines
> that use yield. Most of the code is asnyc, but some of the operations
> are performed in different threads (using concurrent.futures.Executor).
>
> We have concurrent.futures.Future and asyncio.Future. The former one an
> be used to execute code in a different thread, the later one can be
> awaited. Since concurrent.futures.Future does not implement the
> __await__ method, it cannot be awaited in an ioloop. For example, if I
> want to read from a file in a different thread, then I can submit that
> as a task to a ThreadPoolExecutor, but it is not possible to await for
> it. It is still possible to read from the file in the same thread, but
> obviously I do not want to do that (because file read can block the
> loop, and I do not want that).
>
> In other words: this makes it impossible to refactor coroutine functions
> to native coroutine functions.
>
> In my concrete use case, coroutines are used in tornado's ioloop, and
> tornado handles both types of futures *if they are yielded*. But once I
> switch to async def, yield becomes await and the native await is
> incompatible with concurrent.futures.Future.
>
> Both the await keyword and concurrent.futures.Future are in the standard
> library. They should be compatible. Is there a reason why
> concurrent.futures does not implement __await__ ?

My guess: because asyncio wouldn't know what to do with a
concurrent.futures.Future.

The tornado docs say that "You can also use
tornado.gen.convert_yielded to convert anything that would work with
yield into a form that will work with await":
http://www.tornadoweb.org/en/stable/guide/coroutines.html#python-3-5-async-and-await



More information about the Python-list mailing list