【发布时间】:2015-01-22 03:03:58
【问题描述】:
尝试问一个基本的是/否问题——我担心答案是“否”——但如果是“是”,请寻求指导!
我有一个用 Twisted Python 编写的 SMTP 服务器。它工作得非常好!函数调用的基本流程/序列类似于:
script1.py
from mailserver import startReactor, startListener, stopListener, stopReactor
startReactor()
startListener("p25")
startListener("p26")
# Handle incoming connections etc until sigint receivedand then exit cleanly
stopListener("p25")
stopListener("p26")
stopReactor()
我想在一个或多个单独的 python 脚本中做的是:
script2.py
from mailserver import startReactor, startListener, stopListener, stopReactor
startListener("p27")
# Handle incoming connections etc until all necessary stuff on p27 is complete
stopListener("p27")
最后
script3.py
from mailserver import startReactor, startListener, stopListener, stopReactor
stopListener("p25")
startListener("p25custom)
# Handle incoming connections etc until all necessary stuff on "custom" p27 is complete
stopListener("p25custom")
startListener("p25)
所以想法是 script1.py 在后台执行,然后可以执行 script2、script3 等,“更改”正在运行的侦听器列表,但这些侦听器附加到 script1 中的反应器......
通过监控 ps -xaf 和 netstat 我可以看到所有脚本中的套接字都打开了,并且 script1 干净地退出了……但是在 script2 和 script3 中打开的套接字似乎并没有关闭……
在 mailserver.py 中,我维护了一个“runningListeners”(例如:{'p25': <<class 'twisted.internet.tcp.Port'> of mailserver.ConsoleSMTPFactory on 25>})的字典,它在 startListener 和 stopListener 被适当调用时被添加/删除。但是,这显然是 script1 的本地内容,而不是 3 个脚本之间的“共享”字典……而且我非常怀疑在 script2 和 script3 中启动的侦听器实际上与在 script1 中作为 netstat 启动的反应器一样“附加” / ps 可能会建议 - 因此可能不是“可用”的听众......
所以是的,没有的问题 - 是否有可能用多个 python 脚本来做我想做的事情,如果是这样,任何人都可以就我如何实现这一点提供建议吗?
非常感谢!
【问题讨论】:
-
对于“我可以在进程之间共享一个反应器”这个问题的答案是“否”。但是,我认为有一种方法可以使用
spawnProcess来做你想做的任何事情——我只是不知道你真正想要完成的是什么 i> 按您所说的那样运行“多个脚本”。为什么不在主程序中同时运行所有内容? -
嗨@Glyph...我也很害怕。我们可能要做的是让 script1 只运行“默认”侦听器 (p25) - 然后根据需要让额外的反应器随着额外的侦听器启动和停止......有点感觉我们的方式就我们想要什么以及如何获得它而言;-) 干杯!
-
你能在不使用“反应器”这个词的情况下描述你想要什么吗?因为在不启动和停止多个反应器的情况下启动和停止其他 侦听器 非常简单。
-
我们希望按需启动/停止反应器(由于运行并发测试脚本,每个测试脚本都需要配置不同的侦听器 - 接口、端口、SSL 证书等) - 但是,这些测试脚本将从 RobotFramework 测试中运行——因此每个测试都有一个独特的 python 进程——实际上意味着新的侦听器将是“独立的”。这是否阐明了我们想要实现的目标? :)
-
当您执行
listenTCP或endpoint.listen时,您会返回一个IListeningPort对象。如果你坚持你创建的第一个,“标准”监听器,那么你可以调用stopListening,然后再次调用startListening。您的场景中没有任何内容表明第二个过程,除非“机器人框架”(我仍然不知道那是什么!)为您启动一个过程。如果是这样,那么还有一个更根本的问题,即多个并发测试同时对“标准”侦听器是否正在运行有不同的想法。