从正在运行的进程中调用函数
我的程序启动一个子进程,它必须在初始化后向父进程发送某种信号。如果我可以在父级中设置一个处理程序,当发送此信号时调用该处理程序,那就完美了。有什么办法可以做到吗?
阿伦迪特
my programm starts a subprocess, which has to send some kind of signal to the parent after initialization. It would be perfekt if i could set up a handler in parent, which is called when this signal is sent. Is there any way to do it?
Alendit
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 Python 2.6,则可以使用标准库中的
multiprocessing
模块,特别是管道和队列。文档中的简单示例:如果您使用的是 Python 2.4 或 2.5,请不要绝望 - 可以使用向后移植 这里。
If you are using Python 2.6, you can use the
multiprocessing
module from the standard library, in particular pipes and queues. Simple example from the docs:If you are using Python 2.4 or 2.5, don't despair - a backport is available here.
父代码:
子代码:
请小心这种形式的 IPC,因为它有其问题,例如:
我建议阅读整个
signal(2)
手册页。Parent code:
Child code:
Be careful with this form of IPC because it has its issues, e.g.:
I recommend reading the whole
signal(2)
man page.您可以使用 Python 标准库中的 信号模块 来注册信号处理程序。然后子进程将使用正常的信号发送机制。
You can use the signal module from the Python standard library to register a signal handler. The subprocess would then use normal signal sending mechanisms.