[英]Multiple python scripts sending messages to a single central script
I have a number of scripts written in Python 2.6 that can be run arbitrarily. 我有许多用Python 2.6编写的脚本,可以任意运行。 I would like to have a single central script that collects the output and displays it in a single log.
我想有一个中央脚本收集输出并将其显示在一个日志中。
Ideally it would satisfy these requirements: 理想情况下,它将满足这些要求:
I've come across some hints: 我遇到了一些提示:
os.pipe()
multiprocess
mutex
logging.handlers.SocketHandler
(thanks @Dan Head) logging.handlers.SocketHandler
(感谢@Dan Head) From those pieces, I think I could cobble something together. 从这些作品中,我想我可以拼凑一些东西。 Just wondering if there is an obviously 'right' way of doing this, or if I could learn from anyone's mistakes.
只是想知道是否有一种明显的“正确”方式,或者我是否可以从任何人的错误中吸取教训。
I'd consider using logging.handlers.SocketHandler
for the message passing parts of this, it sounds like you have a logging type use case in mind already. 我会考虑使用
logging.handlers.SocketHandler
来传递部分消息,听起来你已经记住了一个日志记录类型用例。
The standard libraries logging facilities are very flexible and configuration driven so you should be able to adapt them to your requirements. 标准库日志记录工具非常灵活且配置驱动,因此您应该能够根据您的要求进行调整。
This doesn't handle the automatic restarting part of your question. 这不能处理问题的自动重启部分。 For UNIX you'd probably just use pid files and
os.kill(pid, 0)
to check if it's running, but I don't know what their equivalents in the Windows world would be. 对于UNIX,您可能只是使用pid文件和
os.kill(pid, 0)
来检查它是否正在运行,但我不知道它们在Windows世界中的等价物是什么。
I built a server to use a Windows named pipe, using the following key code: 我使用以下密钥代码构建了一个使用Windows命名管道的服务器:
def run( self ):
# This is the main server loop for the Win32 platform
import win32pipe
import win32file
self.pipeHandle = win32pipe.CreateNamedPipe(
'\\\\.\\pipe\\myapp_requests',
win32pipe.PIPE_ACCESS_DUPLEX,
win32pipe.PIPE_TYPE_BYTE |
win32pipe.PIPE_READMODE_BYTE |
win32pipe.PIPE_WAIT,
1,
4096,
4096,
10000,
None)
if self.pipeHandle == win32file.INVALID_HANDLE_VALUE:
print 'Failed to create named pipe %s!' % self.pipeName
print 'Exiting...'
sys.exit(1)
while True:
# Open file connection
win32pipe.ConnectNamedPipe( self.pipeHandle )
# Run the main message loop until it exits, usually because
# of a loss of communication on the pipe
try:
self.messageLoop()
except ServerKillSignal:
break
# Return the pipes to their disconnected condition and try again
try: win32pipe.DisconnectNamedPipe( self.pipeHandle )
except: pass
win32file.CloseHandle( self.pipeHandle )
print "Exiting server"
The method messageLoop()
reads data from the pipe, using win32file.ReadFile()
, until win32file.error is thrown. 方法
messageLoop()
使用win32file.ReadFile()
从管道读取数据,直到抛出win32file.error。 Then it exits, allowing run() to restart it. 然后它退出,允许run()重新启动它。
In my implementation, the users were not likely to have administrator access, so this could not be started as a system service. 在我的实现中,用户不太可能具有管理员访问权限,因此无法作为系统服务启动。 Instead, I coded the client to check for the existence of the pipe at '\\.\\pipe\\pyccf_requests'.
相反,我编写了客户端来检查'\\。\\ pipe \\ pyccf_requests'中是否存在管道。 If it doesn't exist, then the client starts a new server process.
如果它不存在,则客户端启动新的服务器进程。
Dan Head's answer is exactly what you want. Dan Head的答案正是你想要的。
Item #2, "If the receiver is not running when the first script tries to send a message, it is started", probably won't work. 项目#2,“如果第一个脚本尝试发送消息时接收器没有运行,它就会启动”,可能无法正常工作。 Something has to be running to receive a message.
必须运行一些东西来接收消息。 I suggest writing a demon process which starts on bootup, and tell Windows to restart it if it dies.
我建议编写一个恶魔进程,它在启动时启动,并告诉Windows如果它死了就重新启动它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.