[Python-Dev] Importance of "async" keyword

Yury Selivanov yselivanov.ml at gmail.com
Fri Jun 26 20:30:25 CEST 2015



On 2015-06-26 1:40 PM, Ethan Furman wrote:
> On 06/26/2015 08:47 AM, Steve Dower wrote:
>> On 06/26/2015 06:48 AM, Sven R. Kunze wrote:
>>
>>> def business():
>>>       return complex_calc(5)
>>>
>>> def business_new()
>>>       return await complex_calc(10)
>
>> Assuming "async def business_new" (to avoid the syntax error), 
>> there's no difference between those functions or the one they're 
>> calling:
>>
>> * "complex_calc" returns an awaitable object that, after you've 
>> awaited it, will result in an int.
>> * "business" returns the return value of "complex_calc", which is an 
>> awaitable object that, after you've awaited it, will result in an int.
>> * "business_new" returns an awaitable object that, after you've 
>> awaited it, will result in an int.
>>
>> In all three of these cases, the result is the same. The fact that 
>> the awaitable object returned from any of them is implemented by a 
>> coroutine isn't important (in the same way that an iterable object 
>> may be implemented by a generator, but it's really irrelevant).
>
> What?  Shouldn't 'business_new' return the int?  It did await, after all. 

"business_new" should be defined with an 'async' keyword, that's where 
all the confusion came from:

   async def business_new():
      return await complex_calc(10)

Now, "business_new()" returns a coroutine (which will resolve to the 
result of "complex_calc" awaitable), "await business_new()" will return 
an int.


Yury


More information about the Python-Dev mailing list