[Python-Dev] PEP 492 quibble and request

Ron Adam ron3200 at gmail.com
Fri May 1 21:49:26 CEST 2015



On 05/01/2015 07:54 AM, Steven D'Aprano wrote:
> On Wed, Apr 29, 2015 at 07:31:22PM -0700, Guido van Rossum wrote:
>
>> >Ah, but here's the other clever bit: it's only interpreted this way
>> >*inside*  a function declared with 'async def'. Outside such functions,
>> >'await' is not a keyword, so that grammar rule doesn't trigger. (Kind of
>> >similar to the way that the print_function __future__ disables the
>> >keyword-ness of 'print', except here it's toggled on or off depending on
>> >whether the nearest surrounding scope is 'async def' or not. The PEP could
>> >probably be clearer about this; it's all hidden in the Transition Plan
>> >section.)
> You mean we could write code like this?
>
> def await(x):
>      ...
>
>
> if condition:
>      async def spam():
>          await (eggs or cheese)
> else:
>      def spam():
>          await(eggs or cheese)
>
>
> I must admit that's kind of cool, but I'm sure I'd regret it.


Actually in the above...

    def await(x):
        return x

Then in any scope where async is used, the keyword will mask the await 
function.


Are the following correct?


Another useful async function might be...

    async def yielding():
        pass

In a routine is taking very long time, just inserting "await yielding()" in 
the long calculation would let other awaitables run.


If the async loop only has one coroutine (awaitable) in it, then it will be 
just like calling a regular function.  No waiting would occur.


-Ron



More information about the Python-Dev mailing list