简体   繁体   English

在 Python 中生成安全、独立于平台的虚拟进程

[英]spawn safe, platform-independent dummy process in Python

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.要为其编写测试,我需要能够从 python 创建进程,这些进程除了等待发出停止信号外什么都不做。 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, eg, 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.另外(这部分可能有点奇怪),如果我能够创建不是创建进程的子进程的进程,那将是我测试的最佳选择,这样我就可以模拟条件,例如os.waitpid不会有权与流程交互,或者一个流程向工厂发出信号以创建流程而不是直接创建流程。

If you're using Python 2.6 the multiprocessing package has some stuff you might find useful.如果您使用的是 Python 2.6,那么多处理 package 有一些您可能会觉得有用的东西。

There's a very simple example on my github .我的 github 上有一个非常简单的例子。 If you run spawner it will create 3 processes that run seperately, but use a channel to talk back to the spawner.如果您运行 spawner,它将创建 3 个单独运行的进程,但使用通道与 spawner 对话。 So if you kill the spawner process the others you have started will die.因此,如果您杀死 spawner 进程,您启动的其他进程将会死亡。 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.恐怕这里有很多冗余代码,我正在重构,但我希望它提供一个基本概念。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM