如何“发出信号”感兴趣的子进程(没有信号)?
我正在尝试找到一种良好且简单的方法来向子进程发出信号 (通过 SocketServer 使用 ForkingMixIn 创建)来自父级 过程。
虽然可以使用 Unix 信号,但我想避免它们,因为只有 有兴趣的孩子应该收到信号,然后就会 要求某种注册太过分且复杂 向父进程识别谁感兴趣的机制。
(请不要建议线程,因为这个特定的程序将无法工作 使用线程,因此必须使用分叉。)
I'm trying to find a good and simple method to signal child processes
(created through SocketServer with ForkingMixIn) from the parent
process.
While Unix signals could be used, I want to avoid them since only
children who are interested should receive the signal, and it would be
overkill and complicated to require some kind of registration
mechanism to identify to the parent process who is interested.
(Please don't suggest threads, as this particular program won't work
with threads, and thus has to use forks.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是 UNIX 系统,因此信号量应该是简单的答案。
不幸的是,python似乎没有提供调用semop系统调用的方法。
如果您使用的是 python 2.6 ,您也许可以使用
多处理模块 条件类。
Since you are on a unix system, semaphores should be the easy answer.
Unfortunately, python does not seem to offer a way to call the semop system call.
If you are using python 2.6 , you may be able to use the
multiprocessing module Condition class.
我提出了使用管道文件描述符的想法,父级可以写入该管道文件描述符,然后与 select 结合读取/刷新,但这并不真正符合一个非常优雅的设计。
更详细地说:父进程将创建一个管道,子进程将继承它,父进程将写入该管道,从而唤醒文件描述符上的任何子进程
select()
:ing,但是然后,parent 会立即从管道的读取端读取数据并将其清空 - 唯一的效果是那些在管道上进行select()
:ing 的子进程已被唤醒向上。正如我所说,这感觉很奇怪而且丑陋,但我还没有找到更好的东西。
编辑:
事实证明,这不起作用 - 有些子进程被唤醒,有些则没有。我求助于使用
multiprocessing
模块中的Condition
。I have come up with the idea of using a pipe file descriptor that the parent could write and then read/flush in combination with select, but this doesn't really qualify as a very elegant design.
In more detail: The parent would create a pipe, the subprocesses would inherit it, the parent process would write to the pipe, thereby waking up any subprocess
select()
:ing on the file descriptor, but the parent would then immediately read from the read end of the pipe and empty it - the only effect being that those child processes that wereselect()
:ing on the pipe have woken up.As I said, this feels odd and ugly, but I haven't found anything really better yet.
Edit:
It turns out that this doesn't work - some child processes are woken up and some aren't. I've resorted to using a
Condition
from themultiprocessing
module.