[Python-Dev] Minimal 'stackless' PEP using generators? (original) (raw)
Clark C. Evans cce at clarkevans.com
Mon Aug 23 20:33:15 CEST 2004
- Previous message: [Python-Dev] Minimal 'stackless' PEP using generators?
- Next message: [Python-Dev] Minimal 'stackless' PEP using generators?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On Mon, Aug 23, 2004 at 01🔞28PM -0400, Phillip J. Eby wrote: | It would be nice if there were some way to "accept" data and exceptions | within a generator that didn't require the 'events.resume' hack, e.g.: || result = yield aDeferred || would be really nice, especially if 'result' could cause an exception to | be raised. I was hoping that this was something along the lines of what | you were proposing.
Perhaps it would be nice to add an alternative syntax to call a generator when you are expecting exactly one value.
def generator():
yield 'one value'
def consumer():
value = generator()
This, when combined with the previous proposal would give:
>>> def top():
>>> yield cooperate
>>> yield "one value"
>>>
>>> def middle():
>>> """ intermediate generator _only_ sees 'one value' """
>>> bing = top()
>>> # do something with bing
>>> yield bing
>>>
>>> def lower():
>>> """ this is not a generator, so it sees cooperate """
>>> for x in middle():
>>> print x
>>>
>>> lower()
cooperate
one value
| Perhaps there should be a "simple coroutines" PEP, that doesn't try to | extend generators into coroutines, but instead treats coroutines as a | first-class animal that just happens to be implemented using some of the | same techniques "under the hood".
The problem is maintaining a 'stack' of generators requires that intermediate generators in the stack need a tedious hack (in both peek.event and twisted.flow) for them to work. If we can have python allow messages to be sent from the top-most generator to the inner-most non-generator (via yield cooperate, or some other magic), this difficulty is resolved -- intermediate generators can then be written in a operational style, without icky hacks for managing deferred execution.
Full-blown corountines arn't necessary. A small tweak to generators will do.
Best,
Clark
- Previous message: [Python-Dev] Minimal 'stackless' PEP using generators?
- Next message: [Python-Dev] Minimal 'stackless' PEP using generators?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]