Question

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?

Était-ce utile?

La solution

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.

Autres conseils

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? E.g.,

# Gracefully stop the reactor
if reactor.running:
    reactor.stop()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top