[Python-ideas] async/await in Python

Guido van Rossum guido at python.org
Mon Apr 20 17:42:59 CEST 2015


On Fri, Apr 17, 2015 at 5:21 PM, Greg Ewing <greg.ewing at canterbury.ac.nz>
wrote:

> Yury Selivanov wrote:
>
>>
>> Here's my proposal to add async/await in Python.
>>
>
> You've essentially reinvented PEP 3152 - Cofunctions.
>
> https://www.python.org/dev/peps/pep-3152/
>

I *thought* Yury's proposal sounded familiar! :-)


> Here's a summary of the relationships between them:
>
> PEP 3152                  Nearest equivalent in Yury's PEP
> --------                  --------------------------------
>
> codef f(args):            async def f(args):
>
> cocall f(args)            await f(args)
>
> __cocall__                __await__
>
> costart()                 async_def()
>
> There is currently no equivalent of "async for" and
> "async with" in PEP 3152, but they could easily be added.
> I would probably spell them "cofor" and "cowith".
>

Unfortunately that's a lot of new reserved words, and they aren't even
words. :-) I like the elegance of Yury's idea of prefixing various
statements with "async". It also happens to easy introduction, since the
lexer can be adapted (for a few releases at least) not to treat "async" as
a reserved word unless followed by "def" etc.


> As the author of PEP 3152 I'm obviously biased, but I
> think my spellings are more elegant and less disruptive
> to reading of the code.
>

As the BDFL I'm perhaps also biased (Yury went over early stages of his
draft with me at PyCon) but I prefer his spellings.


> PEP 3152 is currently marked as deferred. Maybe it's
> time to revive it? If Yury's pep is to be considered,
> we ought to discuss the relative merits of the two.


 I wonder if, instead of putting forward a competing proposal that is so
similar, you could help Yury with PEP 492? I'm sure he'd happily take you
as a co-author.

I have one question about PEP 3152. IIUC, it states that cofunctions cannot
be called except using cocall, and there seems to be syntax that enforces
that the "argument" to cocall looks like a function call. This would rule
out things that are normally always allowed in Python, if you have
```
return foo(args)
```
you can replace that with
```
r = foo(args)
return r
```
But the way I read PEP 3152 the call has to occur syntactically in the
cocall form, so
```
cocall foo(args)
```
cannot be replaced with
```
x = foo(args)
cocall x
```
This ability is occasionally useful in asyncio, for example when creating a
task, we can write
```
x = Task(foo(args))
```
and then someone else can write
```
yield from x
```
Your cocall syntax is lacking such ability, since cocall insist on call
syntax ("cocall x" is a syntax error). It would seem that in general your
cocall is not a substitution for yield from when used with a future or
task, and that looks like a real deficiency to me.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150420/5e605f98/attachment.html>


More information about the Python-ideas mailing list