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

Steve Dower Steve.Dower at microsoft.com
Sat Dec 1 00:32:04 CET 2012


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.


Cheers,
Steve



More information about the Python-ideas mailing list