[Python-Dev] PEP 492: async/await in Python; version 4 (original) (raw)

Guido van Rossum guido at python.org
Tue May 5 22:38:15 CEST 2015


Jumping in to correct one fact.

On Tue, May 5, 2015 at 12:44 PM, Brett Cannon <brett at python.org> wrote:

On Tue, May 5, 2015 at 3:14 PM Paul Moore <p.f.moore at gmail.com> wrote:

Well, twisted always had defertothread. Asyncio has runinexecutor, but that seems to be callback-based rather than coroutine-based?

Yep.

The run_in_executor call is not callback-based -- the confusion probably stems from the name of the function argument ('callback'). It actually returns a Future representing the result (or error) of an operation, where the operation is represented by the function argument. So if you have e.g. a function

def factorial(n):
    return 1 if n <= 0 else n*factorial(n-1)

you can run it in an executor from your async(io) code like this:

loop = asyncio.get_event_loop()
result = yield from loop.run_in_executor(factorial, 100)

(In a PEP 492 coroutine substitute await for yield from.)

-- --Guido van Rossum (python.org/~guido) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20150505/0be2fb96/attachment.html>



More information about the Python-Dev mailing list