[Python-ideas] Making concurrent.futures.Futures awaitable
Guido van Rossum
guido at python.org
Fri Aug 7 19:08:10 CEST 2015
FWIW, I am against this (as Alex already knows), for the same reasons I
didn't like Nick's proposal. Fuzzing the difference between threads and
asyncio tasks is IMO asking for problems -- people will stop understanding
what they are doing and then be bitten when they least need it.
The example code should be written using loop.run_in_executor(). (This
requires that do_something_cpu_heavy() be refactored into a function that
does the work and a wrapper that creates the concurrent.futures.Future.)
On Fri, Aug 7, 2015 at 6:51 PM, Alex Grönholm <alex.gronholm at nextday.fi>
wrote:
> 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
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
--
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150807/fb49e1f0/attachment.html>
More information about the Python-ideas
mailing list