If I create multiple clients by doing this:
def main():
clients = [None]*10
for i in range(0, 10):
clients[i] = ClientFactory()
reactor.connectTCP('192.168.0.1', 8000, clients[i])
reactor.run()
How to I -gracefully- stop the reactor? If I do:
self.transport.loseconnection()
In the protocol, then do:
reactor.stop()
In the factory, then the next client is going to try to come along a stop the reactor again. However, this of course leads to the error:
Can't stop a reactor that isn't running
How can I gracefully stop the reactor in such a scenario?
It's been a while since I did anything with Twisted, but couldn't you just check the value of the reactor.running
property first? Eg,
# Gracefully stop the reactor
if reactor.running:
reactor.stop()
Take your reactor-management code out of your protocol implementation. Replace it with some event-notification code that you can use to learn when the connection has done everything it needs to do. For example, fire a Deferred
.
Then wait on all the deferreds and stop the reactor when they're all done. You might find gatherResults
helpful for this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.