我知道使用 subprocess 模块来隔离可能出现段错误的函数。这有效:
import subprocess
# Blocking for simplicity
res = subprocess.check_output(["python", "c_library_wrapper.py", arg0, arg1, ...])
我想弄清楚的是为什么 multiprocessing 没有同样的效果。这似乎不起作用:
import multiprocessing
from c_library_wrapper import f
# Assume that f puts the return value into a shared queue
p = multiprocessing.Process(target=f, args=(arg0, arg1, ...))
p.start()
p.join()
这不也是创建一个独立的进程吗?我在这里缺少一个核心概念吗?
背景:我正在隔离一个大型第三方 C 库以防止段错误。我知道处理段错误的最佳方法是修复它们,但这个库真的很大。
请您参考如下方法:
您提到您正在使用共享队列。注意 the docs of Process.terminate 中的警告(通过段错误终止是类似的情况):
Warning: If this method is used when the associated process is using a pipe or queue then the pipe or queue is liable to become corrupted and may become unusable by other process. Similarly, if the process has acquired a lock or semaphore etc. then terminating it is liable to cause other processes to deadlock.
