[Python-Dev] Importance of "async" keyword
Yury Selivanov
yselivanov.ml at gmail.com
Wed Jun 24 22:16:52 CEST 2015
Hi Sven,
On 2015-06-24 2:14 PM, Sven R. Kunze wrote:
> Hi everybody,
>
> recently, I stumbled over the new 3.5 release and in doing so over PEP
> 0492.
>
> After careful consideration and after reading many blog posts of
> various coders, I first would like to thank Yury Selivanov and
> everybody else who brought PEP 0492 to its final state. I therefore
> considered usage within our projects, however, still find hazy items
> in PEP 0492. So, I would like to contribute my thoughts on this in
> order to either increase my understanding or even improve Python's
> async capability.
>
> In order to do this, I need a clarification regarding the rationale
> behind the async keyword. The PEP rationalizes its introduction with:
>
> "If useful() [...] would become a regular python function, [...]
> important() would be broken."
>
>
> What bothers me is, why should important() be broken in that case?
Let me first quote the PEP to make the question more obvious.
The section at question is: [1]; it explains why "async" keyword is
needed for function declarations. Here's a code example from the PEP:
def useful():
...
await log(...)
...
def important():
await useful()
Sven, if we don't have 'async def', and instead say that "a function is
a *coroutine function* when it has at least one 'await' expression",
then when you refactor "useful()" by removing the "await" from it, it
stops being a *coroutine function*, which means that it won't return an
*awaitable* anymore. Hence the "await useful()" call in the
"important()" function will be broken.
'async def' guarantees that function always return a "coroutine"; it
eliminates the need of using @asyncio.coroutine decorator (or similar),
which besides making code easier to read, also improves the
performance. Not to mention new 'async for' and 'async with' statements.
This is also covered in the rationale section of the PEP [2]
[1] https://www.python.org/dev/peps/pep-0492/#importance-of-async-keyword
[2] https://www.python.org/dev/peps/pep-0492/#rationale-and-goals
Thanks,
Yury
P.S. This and many other things were discussed at length on the mailing
lists, I suggest you to browse through the archives.
More information about the Python-Dev
mailing list