[Twisted-Python] using a reactor in a loop

Jean-Paul Calderone exarkun at divmod.com
Thu Mar 1 10:32:51 MST 2007


On Thu, 1 Mar 2007 09:21:50 -0800, Tom Brown <brown at esteem.com> wrote:
>Hello,
>
>I am trying to use a reactor in a loop. The application will go through the
>loop twice then hang when trying to stop the protocol. Here's my loop:
>
> [snip]
>
>I see the 'reactor stopped' get printed out twice through the loop but I don't
>see 'running server' get printed out a third time. So, reactor.run() is not
>returning. I must be missing something or going at this completely wrong. Any
>suggestions?

The simple thing you're missing is that reactors can't be restarted. :)  One
way you can restructure your program to avoid needing to do this is to put
the body of your loop (minus reactor.run()) into a function which returns a
Deferred and to replace the call to reactor.stop() with code which fires that
Deferred.  You can set up callbacks on this Deferred to execute the whole
thing again if necessary.

Jean-Paul




More information about the Twisted-Python mailing list