Python主进程与fork子进程GC信息共享机制及范围探究
多进程中对象的生命周期与GC行为分析
核心结论
- 采用
spawn启动方式时,对象会被克隆,每个进程中的对象会各自被终结 - 采用
fork/forkserver启动方式时,对象会被共享,仅在主进程中被终结
验证代码
import os from multiprocessing import Process import multiprocessing multiprocessing.set_start_method('fork', force=True) class Track: def __init__(self): print(f'{os.getpid()=} object created in {__name__=}') def __getstate__(self): print(f'{os.getpid()=} object pickled in {__name__=}') return {} def __setstate__(self, state): print(f'{os.getpid()=} object unpickled in {__name__=}') return self def __del__(self): print(f'{os.getpid()=} object deleted in {__name__=}') def f(x): print(f'{os.getpid()=} function executed in __name__={__name__}') if __name__ == '__main__': x = Track() for i in range(2): print(f'{os.getpid()=} Iteration: {i}, Process object created') p = Process(target=f, args=(x,)) print(f'{os.getpid()=} Iteration: {i}, Process created and started') p.start() print(f'{os.getpid()=} Iteration: {i}, Process starts to run functions') p.join()
测试输出
os.getpid()=30620 object created in __name__='__main__' os.getpid()=30620 Iteration: 0, Process object created os.getpid()=30620 Iteration: 0, Process created and started os.getpid()=30620 Iteration: 0, Process starts to run functions os.getpid()=30623 function executed in __name__='__main__' os.getpid()=30620 Iteration: 1, Process object created os.getpid()=30620 Iteration: 1, Process created and started os.getpid()=30620 Iteration: 1, Process starts to run functions os.getpid()=30624 function executed in __name__='__main__' os.getpid()=30620 object deleted in __name__='__main__'
技术疑问
- 尽管子进程由主进程fork而来,但fork后二者为独立进程,它们如何实现GC信息的共享?
- 这种GC信息共享是针对所有对象,还是仅针对作为子进程参数传递的对象?
内容的提问来源于Stack Exchange,提问作者youkaichao
相关产品推荐
相关产品推荐

