Yield inside try...finally

Peter Hansen peter at engcorp.com
Wed Jun 4 15:05:54 CEST 2003

Michael Sparks wrote:
> Thanks to all who responded. I'll group my replies here...
> Peter Hansen <peter at engcorp.com> wrote:
> > Twisted does "deliberate wheel reinvention" already, in many
> > different areas.  Why reinvent reinventing the wheel when others
> > already do it so well?  ;-)

Note the wink: I was actually joking because you said "don't say
Twisted, I'm deliberately reinventing the wheel", but I've seen
a number of people say the Twisted folks like to reinvent wheels...
(in case that wasn't obvious).
> Twisted is, AFAICT, essentially a state machine/reactor based approach
> which has been done multiple times in multiple languages, each time with
> a different "twist". (sorry...)

Sounds about right, for a fairly broad definition of "state machine",
but that will do for a start.

> In the past I've worked as part of a large team supporting and helping
> maintain a million line piece high performance network code written in
> C++ that was entire state machine based. The system was elegant, worked
> very  efficiently, but was _fragile_ to change, and needed a high skill
> level to debug due to this fragilty.

Could be... I have chosen to use Twisted so far, and steadily to increase 
my use of it, precisely because the results appear to be very robust, 
and efficient, and definitely *not* fragile.  

> The complaints may not apply to Twisted, but in most statemachine /
> reactor based systems I've come across, sooner or later things become
> either ugly, fragile or inefficient. (pick any one :)

Probably true.  I believe Twisted rather stands out in this area so far
(in terms of not being ugly, fragile, or inefficient that is).

> This wheel re-invention is designed to see if using a generator based
> (or rather co-routine) approach produces clearer, more readable, more
> maintainable code, with significantly less fragility. It might not pan
> out, but then again, it might. (After all state machines and generators
> are functionally equivalent.)

Sounds very interesting, especially in light of Alan Kennedy's thread
on "Help with coroutine-based state machines".  

I only respond this time to mention "Flow", a recent and perhaps still
slightly experimental addition to Twisted, which appears to be designed
to use generators to implement very robust and clean protocols, perhaps
along the lines you are thinking.  I don't know if any of them manage
a yield inside a try/finally, but you might take a look at the code
(probably only in CVS at this point, but you could check the mailing
list archives for some interesting details).


More information about the Python-list mailing list