[PEAK] Events Question: How bad of an idea is this?

Phillip J. Eby pje at telecommunity.com
Wed Nov 10 19:59:54 EST 2004

At 04:24 PM 11/10/04 -0800, Chad Rosenberg wrote:
>The code I have been working on uses quite a bit of generators wrapped by
>taskFactory and I am finding it increasing necessary to be able to call a
>variety of other nested methods from those generators that make calls
>involving Twisted Deferreds and EventLoop.runUntil in twisted_support.py.

Twisted doesn't support nested 'runUntil()'; that's why you're having a 
problem.  Maybe the Twisted gurus can speak up about this, but IIRC 
'reactor.run()' is not re-entrant; you can't call it from within 
'reactor.run()', and this makes nested 'runUntil()' not a good idea on a 
Twisted reactor.

Actually, nesting an event loop is in general a wizard-level activity, and 
I personally wouldn't do it except when really, *really* necessary.  I 
should probably add some warnings to the 'runUntil()' docs.

>Trying to get it to work, I made the following change to
>EventLoop.runUntil in twisted_support.py:
>         if not exit:
>             self.reactor.run(False)
>   -- to --
>         while not exit:
>             self.reactor.run(False)
>With this change the above sample code runs to completion without any
>errors, as does the code I am working with.  I'm not sure of what, if any,
>long term repercussion this may produce (stack overflows, infinite loops,
>etc).  Hence my question, how bad of an idea is this?

Hm.  Well, that depends on what you're doing with the Twisted side of 
things.  Keep in mind that when you use a nested 'runUntil()', the reactor 
is going to fire shutdown events as soon as the *inner* runUntil() 
finishes.  This may not be what you want, since I believe that it will 
close all current socket connections.  Personally, I don't think that this 
would be a useful feature to add to PEAK.  :)

More information about the PEAK mailing list