Python扭曲了停止具有多个客户端的Reactor

Python扭曲了停止具有多个客户端的Reactor

本文介绍了Python扭曲了停止具有多个客户端的Reactor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我通过这样做创建了多个客户:

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?

推荐答案

从您的协议实现中删除反应堆管理代码。将其替换为一些事件通知代码,您可以使用它们来了解连接何时已完成其需要做的所有事情。例如,发射 Deferred

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.

这篇关于Python扭曲了停止具有多个客户端的Reactor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:56