[Python-ideas] An async facade? (was Re: [Python-Dev] Socket timeout and completion based sockets)

Guido van Rossum guido at python.org
Sat Dec 1 00:48:23 CET 2012


On Fri, Nov 30, 2012 at 3:32 PM, Steve Dower <Steve.Dower at microsoft.com> wrote:
> Guido van Rossum wrote:
>> Greg Ewing wrote:
>>> Guido van Rossum wrote:
>>>>
>>>> Futures or callbacks, that's the question...
>>>>
>>>> Richard and I have even been considering APIs like this:
>>>>
>>>> res = obj.some_call(<args>)
>>>> if isinstance(res, Future):
>>>>     res = yield res
>>>
>>>
>>> I thought you had decided against the idea of yielding futures?
>>
>> As a user-facing API style, yes. But this is meant for an internal API
>> -- the equivalent of your bare 'yield'. If you want to, I can consider another style as well
>>
>>
>> res = obj.some_call(<args>)
>> if isinstance(res, Future):
>>     res.<magic_call>()
>>     yield
>>
>> But I don't see a fundamental advantage to this.
>
> I do, it completely avoids ever using yield from to pass values around when used for coroutines.
>
> If values are always yielded or never yielded then it is easy (or easier) to detect errors such as:
>
> def func():
>     data = yield from get_data_async()
>     for x in data:
>         yield x
>
> When values are sometimes yielded and sometimes not, it's much harder to reliably throw an error when a value was yielded. Always using bare yields lets the code calling __next__() (I forget whether we're calling this "scheduler"...) raise an error if the value is not None.

Good point. I'll keep this in mind.

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list