在 Python 中生成安全、独立于平台的虚拟进程
我正在编写一些需要在不同操作系统平台上运行并与单独进程交互的代码。为了为其编写测试,我需要能够从 python 创建进程,这些进程除了等待收到停止信号外什么都不做。我希望能够创建一些递归创建更多进程的进程。
另外(这部分可能有点奇怪),如果我能够创建不是创建进程的子进程的进程,那对我的测试来说是最好的,这样我就可以模拟条件,例如 os.waitpid 将无权与流程交互,或者一个流程向工厂发出信号以创建流程而不是直接创建流程。
I'm writing some code that needs to run on different OS platforms and interact with separate processes. To write tests for it, I need to be able to create processes from python that do nothing but wait to be signaled to stop. I would like to be able to create some processes that recursively create more.
Also (this part might be a little strange), it would be best for my testing if I were able to create processes that weren't children of the creating process, so I could emulate conditions where, e.g., os.waitpid
won't have permission to interact with the process, or where one process signals a factory to create a process rather than creating it directly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用的是 Python 2.6,则多处理包有一些您可能会觉得有用的东西。
我的 github 上有一个非常简单的示例。如果你运行spawner,它将创建3个单独运行的进程,但使用一个通道与spawner对话。因此,如果您终止了生成器进程,您启动的其他进程也会死亡。恐怕这里有很多冗余代码,我正在进行重构,但我希望它能给出一个基本的想法。
If you're using Python 2.6 the multiprocessing package has some stuff you might find useful.
There's a very simple example on my github. If you run spawner it will create 3 processes that run seperately, but use a channel to talk back to the spawner. So if you kill the spawner process the others you have started will die. I'm afraid there's a lot of redundant code in here, I'm in the middle of a refactoring, but I hope it gives a basic idea.