[Python-Dev] Importance of "async" keyword

Steven D'Aprano steve at pearwood.info
Thu Jun 25 04:16:59 CEST 2015


On Wed, Jun 24, 2015 at 11:21:54PM +0200, Sven R. Kunze wrote:
> Thanks, Yury, for you quick response.
> 
> On 24.06.2015 22:16, Yury Selivanov wrote:
> >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.
> 
> I feared you would say that. Your reasoning assumes that *await* needs 
> an *explicitly declared awaitable*.
> 
> Let us assume for a moment, we had no async keyword. So, any awaitable 
> needs to have at least 1 await in it. Why can we not have awaitables 
> with 0 awaits in them?

I haven't been following the async discussion in detail, but I would 
expect that the answer is for the same reason that you cannot have a 
generator function with 0 yields in it.


> >'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.
> 
> Recently, I read Guido's blog about the history of Python and how he 
> eliminated special cases from Python step by step. As I see it, the same 
> could be done here.
> 
> What is the difference of a function (no awaits) or an awaitable (> 1 
> awaits) from an end-user's perspective (i.e. the programmer)?

The first is syncronous, the second is asyncronous. 

> My answer would be: none. When used the same way, they should behave in 
> the same manner. As long as, we get our nice tracebacks when something 
> went wrong, everything seems find to me.

How is that possible?

def func():
    # Simulate a time consuming calculation.
    time.sleep(10000)
    return 42

# Call func syncronously, blocking until the calculation is done:
x = func()
# Call func asyncronously, without blocking:
y = func()


I think that one of us is missing something here. As I said, I haven't 
followed the whole discussion, so it might be me. But on face value, I 
don't think what you say is reasonable.

> >P.S. This and many other things were discussed at length on the 
> >mailing lists, I suggest you to browse through the archives.
> 
> I can imagine that. As said I went through of some of them, but it could 
> be that I missed some of them as well.
> 
> Is there a way to search them through (by not using Google)?

You can download the mailing list archive for the relevant months, and 
use your mail client to search them.


-- 
Steve


More information about the Python-Dev mailing list