python - 在多个端口上运行 Reactor.run

标签 python twisted

我有以下代码。我试图让 react 器在从 1025 + NUM_TABLES 开始的端口上运行。问题是当我在循环中调用 reactor.run() 时,循环卡住了。当它被注释时,循环从 0 到 9 计数,但当它被取消注释时,它只打印 0。

NUM_TABLES = 10

factories = [ ]
for i in range(0, NUM_TABLES):
    print i
    factory = Factory()
    factory.protocol = Socket
    factory.clients = []
    factories.append(factory)
    reactor.listenTCP(1025+i, factory)
    #print "Blackjack server started"
    reactor.run()

如何才能在不同的港口经营一家新工厂?为什么reactor停在0?以前有用,为什么现在不行了?谢谢。

最佳答案

在创建并注册所有监听器之前,不要调用 reactor.run()。这应该是您直接从主脚本进行的最后一次调用;之后的一切都是回调。

澄清一下,只需取消最后一行的缩进一级即可。

关于python - 在多个端口上运行 Reactor.run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10866343/

相关文章:

python - 在 Lion 上为 python 安装 twisted 失败

python - 如何在扭曲中为某些文件打开新的日志文件

python - 如何在 Pandas DataFrame 中组合范围和单个/数组列选择

python - 在 Python 中将多行字符串作为一行读取

python - 如何在数据框列中过滤并仅保留 6 位数字

Python扭曲的irc : Wait for a whois reply inside privmsg method

python : Clear the buffer in Twisted Transport write

python - 如何在 Python 中使用带有 gdcm 的 DICOM LUT 解码器 - 传递缓冲区

python - 根据条件将多个列值设置为 NaN

twisted - 在 render_GET 或 render_POST 处理期间发布另一个 Web 查询