[Twisted-Python] Calling reactor.run() / reactor.stop() twice

John Dawson jdawson at io.com
Tue Nov 13 17:47:12 EST 2007

I know it's a bizarre thing to want.  I've got a client/server system
using Twisted.  The client doesn't do anything asynchronously, except,
of course, for its interaction with the server via Perspective Broker.
 The way the code is written right now, the code is only starts up the
reactor and interacts with the server when it needs to send an RPC to
the server.  Until now, it would only ever do (at most) one RPC to the
server.  But I now need to do two.  It would have been simpler to fix
the client code if the reactor were restartable.  But given your
feedback, it looks I'll have to restructure things a bit to
accommodate having a long-running reactor.

The client pseudocode looks something like this:

    def start_server():
        # Fire off an ssh command to run the server on the remote host

    def stop_server():
        # start reactor
        # connect to server
        # send PB message to server to tell it to shut down
        # stop reactor

    def get_server_status():
        # start reactor
        # connect to server
        # stop reactor
        # return True/False of whether connect was successful

    def restart_server():
        if get_server_status():

But, it's certainly fixable to have just one run/stop of the reactor loop.

Thanks for help!

On 11/13/07, Justin Warren <daedalus at eigenmagic.com> wrote:
> On Tue, 2007-11-13 at 15:43 -0600, John Dawson wrote:
> > Hi,
> >
> > The following Twisted code hangs, since the second call to
> > reactor.stop() doesn't seem to really stop the reactor.  Is this a bug
> > in Twisted, or is it impermissible to run/stop a reactor more than
> > once?
> The reactor is not restartable. Attempting to restart the reactor will
> result in unpredictable behaviour.
> Why do you want to do this?
> --
> Justin Warren <daedalus at eigenmagic.com>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

John Dawson <jdawson at io.com>

More information about the Twisted-Python mailing list