[Python-ideas] A send() built-in function to drive coroutines

Luciano Ramalho luciano at ramalho.org
Mon Feb 23 14:22:05 CET 2015


On Mon, Feb 23, 2015 at 10:12 AM, Victor Stinner
<victor.stinner at gmail.com> wrote:
> The use case for your send() function is unclear to me. Why not using
> send() directly?

Bacause the generator may not have started yet, so gen.send() would fail.

> inspect.getgeneratorstate(generator) == 'GEN_CREATED' test looks weird
> (why do you need it?).

To know that the generator must be primed.

> You should already know if the generator
> started or not.

I would, if I had written the generator myself. But my code may need
to work with a generator that was created by some code that I do not
control.

Thanks for your interest, Victor.

Best,

Luciano




>
> Victor
>
> 2015-02-23 12:23 GMT+01:00 Luciano Ramalho <luciano at ramalho.org>:
>> On Mon, Feb 23, 2015 at 7:49 AM, Victor Stinner
>> <victor.stinner at gmail.com> wrote:
>>> 2015-02-16 13:53 GMT+01:00 Luciano Ramalho <luciano at ramalho.org>:
>>>> At a high level, the behavior of send() would be like this:
>>>>
>>>> def send(coroutine, value):
>>>>     if inspect.getgeneratorstate() == 'GEN_CREATED':
>>>>         next(coroutine)
>>>>     coroutine.send(value)
>>>
>>> It's strange to have to sometimes run one iterations of the generator,
>>> sometimes two iterations.
>>
>> The idea is to handle generators that may or may not be primed. I
>> updated that snippet, it now reads like this:
>>
>> https://gist.github.com/ramalho/c1f7df10308a4bd67198#file-send_builtin-py-L43
>>
>>> asyncio.Task is a nice wrapper on top of coroutines, you never use
>>> coro.send() explicitly. It makes coroutines easier to use.
>>
>> Yes it is, thanks! My intent was to build something that was not tied
>> to the asyncio event loop, to make coroutines in general easier to
>> use.
>>
>> Thanks for your response, Victor.
>>
>> Best,
>>
>> Luciano
>>
>> --
>> Luciano Ramalho
>> Twitter: @ramalhoorg
>>
>> Professor em: http://python.pro.br
>> Twitter: @pythonprobr



-- 
Luciano Ramalho
Twitter: @ramalhoorg

Professor em: http://python.pro.br
Twitter: @pythonprobr


More information about the Python-ideas mailing list