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

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? #!/opt/local/bin/python2.5 from twisted.internet import reactor def ping(): def stop_reactor(): print "stopping reactor" reactor.stop() print "Ping: ENTER" print "Setting up timer for callback" reactor.callLater(0.25, stop_reactor) print "Running reactor" reactor.run() print "Reactor stopped" print "Ping: EXIT" print "ping1" ping() print print "ping2" ping() It prints: $ ./reactor1.py ping1 Ping: ENTER Setting up timer for callback Running reactor stopping reactor Reactor stopped Ping: EXIT ping2 Ping: ENTER Setting up timer for callback Running reactor stopping reactor And then hangs. Any help/advice appreciated. John -- John Dawson <jdawson@io.com>

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@eigenmagic.com>

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(): stop_server() start_server() But, it's certainly fixable to have just one run/stop of the reactor loop. Thanks for help! John On 11/13/07, Justin Warren <daedalus@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@eigenmagic.com>
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- John Dawson <jdawson@io.com>

On Tue, 13 Nov 2007 15:43:23 -0600, John Dawson <jdawson@io.com> 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 latter. Jean-Paul
participants (3)
-
Jean-Paul Calderone
-
John Dawson
-
Justin Warren