简体   繁体   English

跨python进程共享的Python Twisted反应器?

[英]Python Twisted reactor shared across python processes?

Trying to ask a basic yes / no question - to which the answer I fear is "no" - but if it is a "yes", seeking guidance on how! 尝试问一个基本的是/否问题-我担心的答案是“否”,但如果是“是”,请寻求有关如何操作的指导!

I have an SMTP server written in Twisted Python. 我有一个用Twisted Python编写的SMTP服务器。 It works very nicely! 效果很好! Basic flow / sequence of function calls is something like: 基本的流程/函数调用顺序类似于:

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()

What I would like to do, in one or more separate python scripts (well, actually it'll be RobotFramework tests, but the principal is the same) is: 我想在一个或多个单独的python脚本中做的(实际上,这将是RobotFramework测试,但原理是相同的)是:

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")

And finally 最后

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)

So the idea is that script1.py is executed in the background,and then script2, script3 etc can be executed, "changing" the list of running listeners, but with these listeners attached to the reactor in script1.... 因此,想法是在后台执行script1.py,然后可以执行script2,script3等,“更改”正在运行的侦听器列表,但这些侦听器已附加到script1中的反应堆上。

By monitoring ps -xaf and netstat I can see the sockets opening in all scripts, and script1 exits cleanly ... but the sockets opened in script2 and script3 don't seem to close down... 通过监视ps -xaf和netstat,我可以看到所有脚本中的套接字都已打开,而script1干净退出了……但是在script2和script3中打开的套接字似乎并没有关闭...

In mailserver.py I maintain a dict of "runningListeners" (eg: {'p25': <<class 'twisted.internet.tcp.Port'> of mailserver.ConsoleSMTPFactory on 25>} ) which gets added to / deleted from as startListener and stopListener get called as appropriate. 在mailserver.py中,我维护了一个“ runningListeners”的字典(例如: {'p25': <<class 'twisted.internet.tcp.Port'> of mailserver.ConsoleSMTPFactory on 25>} ))被添加为适当地调用startListener和stopListener。 However, this is obviously local to just script1, and not a "shared" dict between the 3 scripts... And I very much doubt the listeners started in script2 and script3 are actually as "attached" to the reactor started in script1 as netstat / ps might suggest - and as such probably not "useable" listeners... 但是,这显然仅是script1的本地语言,而不是3个脚本之间的“共享”字典……而且我非常怀疑在script2和script3中启动的侦听器是否实际上“附加”到了在script1中作为netstat启动的反应堆/ ps可能会建议-因此可能不是“可用”的侦听器...

So the yes no question - is it even possible to do what I am trying to do with multiple python scripts, and if so, can anyone offer a suggestion on how I can achieve this? 因此,是的,没有问题-甚至可以使用多个python脚本来做我想做的事情,如果是这样,那么有人可以提出关于如何实现此目标的建议吗?

Many Thanks! 非常感谢!

One solution would be to make script1 'smarter' so that it runs the one single twisted reactor, but also have it running additional polling code that monitors either the file system or the network for bespoke 'instructions' sent from the other two scripts. 一种解决方案是使script1“更智能”,以便它可以运行一个扭曲的反应堆,而且还可以运行附加的轮询代码,以监视文件系统或网络中是否有其他两个脚本发送的定制“指令”。

The instructions from the other two scripts would then instruct script1 about what to listen for (eg what ports etc to listen on and what handler code it should to run when an event occurs, possibly delegating using the subprocess module) and also indicate when a listener should be stopped. 然后,来自其他两个脚本的指令将指示script1有关侦听的内容(例如,侦听的端口等以及事件发生时应运行的处理程序代码,可能使用子流程模块委派),并还指示侦听器的时间。应该停止。

So I guess the answer is erm yes, but its not easy so you may wish to reconsider your design first. 因此,我想答案是肯定的,但这并不容易,因此您不妨先重新考虑一下设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM