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

Luciano Ramalho luciano at ramalho.org
Mon Feb 23 12:23:29 CET 2015


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


More information about the Python-ideas mailing list