[Python-ideas] A send() built-in function to drive coroutines
Ron Adam
ron3200 at gmail.com
Mon Feb 23 16:55:39 CET 2015
On 02/23/2015 08:22 AM, Luciano Ramalho wrote:
> 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.
When using coroutines, they are generally run by a co-routine framework.
That is how they become "CO"-routines rather than just generators that you
can send values to. Co-routines generally use the yield to pause/continue
and to communicate to the framework, and not to get and receive data. In
most case I've seen co-routines look and work very much like functions
except they have yield put in places so they cooperate with the frame work
that is running them. That frame work handles the starting co-routine.
I think what you are referring to is generators that are both providers and
consumers, and not co-routines.
Cheers,
Ron
More information about the Python-ideas
mailing list