簡體   English   中英

跨python進程共享的Python Twisted反應器?

[英]Python Twisted reactor shared across python processes?

嘗試問一個基本的是/否問題-我擔心的答案是“否”,但如果是“是”,請尋求有關如何操作的指導!

我有一個用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腳本中做的(實際上,這將是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")

最后

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腳本來做我想做的事情,如果是這樣,那么有人可以提出關於如何實現此目標的建議嗎?

非常感謝!

一種解決方案是使script1“更智能”,以便它可以運行一個扭曲的反應堆,而且還可以運行附加的輪詢代碼,以監視文件系統或網絡中是否有其他兩個腳本發送的定制“指令”。

然后,來自其他兩個腳本的指令將指示script1有關偵聽的內容(例如,偵聽的端口等以及事件發生時應運行的處理程序代碼,可能使用子流程模塊委派),並還指示偵聽器的時間。應該停止。

因此,我想答案是肯定的,但這並不容易,因此您不妨先重新考慮一下設計。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM