简体   繁体   English

多个python脚本将消息发送到单个中央脚本

[英]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: 理想情况下,它将满足这些要求:

  • Every script sends its messages to the same "receiver" for display. 每个脚本都将其消息发送到同一个“接收器”进行显示。
  • If the receiver is not running when the first script tries to send a message, it is started. 如果在第一个脚本尝试发送消息时接收器未运行,则会启动它。
  • The receiver can also be launched and ended manually. 接收器也可以手动启动和结束。 (Though if ended, it will restart if another script tries to send a message.) (虽然如果结束,如果另一个脚本尝试发送消息,它将重新启动。)
  • The scripts can be run in any order, even simultaneously. 脚本可以按任何顺序运行,甚至可以同时运行。
  • Runs on Windows . Windows上运行。 Multiplatform is better, but at least it needs to work on Windows. 多平台更好,但至少它需要在Windows上运行。

I've come across some hints: 我遇到了一些提示:

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.

相关问题 从 python 中的单个脚本运行多个脚本 - Running multiple scripts from a single Script in python 如何使用单个 python(.py) 脚本运行多个 python 脚本 - How to run multiple python scripts using single python(.py) script 同时发送消息python脚本 - sending out messages simultaneously python script 从一个脚本运行多个python脚本,并在它们之间来回通信? - Running multiple python scripts from a single script, and communicating back and forth between them? 如何在带有 args 的 python 脚本中运行多个脚本 - How to run multiple scripts in a python script with args 编写脚本以在Python中运行多个脚本 - Writing a script to run multiple scripts in Python 如何从python中的脚本运行多个脚本 - How to run multiple scripts from a script in python 带有硒的 Python 脚本,用于在 textnow 上发送文本消息不起作用 - Python Script with selenium for sending text messages on textnow not working 使用我的python脚本获取多个错误消息 - Getting multiple error messages with my python script Python Twilio / Watchguard脚本发送2条消息,而不是预期的1条消息 - Python Twilio/Watchguard script sending 2 messages rather than the 1 expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM