[Python-ideas] Making concurrent.futures.Futures awaitable

Alex Grönholm alex.gronholm at nextday.fi
Fri Aug 7 18:51:57 CEST 2015


There's an open issue for adding support for awaiting for 
concurrent.futures.Futures here:
http://bugs.python.org/issue24383

This is about writing code like this:

async def handler(self):

     result = await some_blocking_api.do_something_cpu_heavy()

     await self.write(result)


As it stands, without this feature, some boilerplate is required:

from asyncio import wrap_future

async def handler(self):

     result = await wrap_future(some_blocking_api.do_something_cpu_heavy())

     await self.write(result)


I wrote a patch (with tests by Yury Selivanov) that adds __await__() to 
concurrent.futures.Future
and augments the asyncio Task class to handle concurrent Futures.

My arguments on why we should add this:

  * it eliminates the boilerplate code, reducing complexity
  * it also makes concurrent Futures work with "yield from" style
    non-native coroutines
  * it does not interfere with any existing functionality
  * standard library components should work with each other

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150807/9b21b615/attachment-0001.html>


More information about the Python-ideas mailing list