I have a few callback functions and I'd like to launch as multiple processes and have them all terminate via signal from the parent process.
My current way of doing this is creating a shared c_bool with multiprocessing.Value
and setting it to True
, then distributing it to all of my processes when they are created. My processes all run a while loop using the shared bool like so:
while myC_bool: ...keep running...
I can then just switch the bool to False
from my parent process and all child processes will complete their final loop and exit.
很多人都告诉过我,并且在文档中阅读过在使用多处理时应该尽量避免使用共享内存。有人告诉我避免这种情况的最佳方法是守护进程,给它一个自定义信号处理程序并向它发送一个 sigint/sigterm/etc...
我的问题是,是否专门使用 bool 来保持循环处于活动状态,并且只能从我的父进程中更改它的值,并从多个子进程中读取它是一个合适的解决方案,以使我的所有子进程快速安全地终止?我觉得所有孩子只看一个共享布尔值的开销比向他们发送 x 个信号要少。
守护进程会是更好的解决方案吗?如果是这样,我需要一些帮助来理解为什么。