简体   繁体   English

使用多处理在预先存在的Python进程之间进行通信

[英]Communicating between Pre-Existing Python Processes using Multiprocessing

Is it possible to use 'multiprocessing' communication to a process that I didn't spawn? 是否可以将“多处理”通信用于未产生的进程?

The 'multiprocessing' module looks ideal for my needs, but in all the examples one process spawns the other. “多处理”模块看起来很适合我的需求,但是在所有示例中,一个过程都产生了另一个。 How can I get a 'handle' on the process I want to communicate with if I didn't spawn it? 如果没有生成,如何在要与之通信的流程上获得“句柄”?

Background Information 背景资料

There are three processes in total - all Python 2.7 - a worker, a watchdog, and a web server. 总共有三个进程-所有Python 2.7-一个工作程序,一个看门狗和一个Web服务器。

The worker is a long-running test system. 该工作人员是一个长期运行的测试系统。 I want the web server to be able to query the worker to see how it is progressing. 我希望Web服务器能够查询工作程序以查看其进度。 As the worker may often crash, a watchdog process restarts it from time to time (and hence it will have a new PID). 由于工作人员可能经常崩溃,看门狗进程会不时重新启动它(因此它将有一个新的PID)。

I have access to the source of the worker and the watchdog, but I want to modify those as little as possible. 我可以访问工作人员和看门狗的来源,但是我想尽可能少地修改它们。

My idea is to have the web server query the worker periodically and share, say, a dict. 我的想法是让Web服务器定期查询工作人员并共享一个命令。

From what you've described, you want the "worker", which may crash and be restarted, to publish some dictionary of data to the "web server." 根据您的描述,您希望可能会崩溃并重新启动的“工作者”将一些数据字典发布到“ Web服务器”。 I think the easiest way to do this is to simply have the web server accept incoming messages from the worker via HTTP PUT. 我认为最简单的方法是让Web服务器通过HTTP PUT接受来自工作程序的传入消息。 You can encode the worker's dict as JSON and put that in the HTTP body. 您可以将工作者的字典编码为JSON并将其放在HTTP正文中。 This makes use of the protocols you are likely already using, and avoids having to coordinate restarts of the worker to rediscover it when its PID changes. 这利用了您可能已经在使用的协议,并且避免了必须协调工作程序的重新启动以在其PID更改时重新发现它。

Of course, if the server hasn't seen an update after a certain amount of time, it can consider the worker to be dead. 当然,如果服务器在一定时间后仍未看到更新,则可以认为该工作程序已死。 It may even be able to subsume the watchdog functionality therefore. 因此,它甚至甚至可以包含看门狗功能。

Is it possible to use 'multiprocessing' communication to a process that I didn't spawn? 是否可以将“多处理”通信用于未产生的进程?

No. The multiprocessing module can work the way it does because the processes 'know' their relation 'behind the scenes' even though you don't see it. 不会。 multiprocessing模块可以按其方式工作,因为即使您没有看到,这些过程也“知道”它们在幕后的关系。

There are a couple of possible solutions; 有两种可能的解决方案。

  1. Fold the watchdog into the web server. 将看门狗折叠到Web服务器中。 Then you can use multiprocessing to launch the worker and communicate between the worker and web server. 然后,您可以使用多处理来启动工作服务器并在工作服务器与Web服务器之间进行通信。
  2. Use IPC, eg sockets . 使用IPC,例如sockets Make the webserver listen on a seperate port for connections from the worker. 使Web服务器在单独的端口上侦听来自worker的连接。 Using a host and port combination, socket.getaddrinfo() will return the info needed for a connection. 使用主机和端口的组合, socket.getaddrinfo()将返回连接所需的信息。
  3. Use a specially crafted HTTP PUT request to send data from the worker to the web server as John Zwick explained. 如John Zwick所述,使用特制的HTTP PUT请求将数据从工作程序发送到Web服务器。

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

相关问题 进程之间进行通信:Python多处理 - Communicating between Processes: Python Multiprocessing 进程间通信 Python - Communicating between processes Python 在 Windows 7 中与 USB 设备通信并仍然使用预先存在的驱动程序? - Communicating to USB devices in windows 7 and still use pre-existing drivers? 使用 gtts 覆盖 Python 中预先存在的 .mp3 文件 - Overwrite pre-existing .mp3 file in Python using gtts Append 到 Python 中预先存在的键的值 - Append to value of pre-existing keys in Python 在SWIG中使用CMake进行Python中的c ++扩展,并依赖于预先存在的库 - Using CMake for a c++ extension in Python using SWIG with dependencies on pre-existing libraries 在python多处理中的对象之间进行通信和比较? - Communicating and comparing between objects in python-multiprocessing? 使用 dataframe 中 3 个预先存在的列中的“def”编写 python function; 第 1 列和第 2 列作为输入 = 第 3 列作为 output - write a python function using ```def``` from 3 pre-existing columns in a dataframe; columns 1 and 2 as inputs = column 3 as output 使用Flask,python和postgresql如何连接到预先存在的数据库? - using Flask, python and postgresql how can I connect to a pre-existing database? 使用循环从现有数组的元素生成新数组[Python] - Using a loop to generate new arrays from elements of pre-existing arrays [Python]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM