You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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__'

技术疑问

  1. 尽管子进程由主进程fork而来,但fork后二者为独立进程,它们如何实现GC信息的共享?
  2. 这种GC信息共享是针对所有对象,还是仅针对作为子进程参数传递的对象?

内容的提问来源于Stack Exchange,提问作者youkaichao

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 18:52:51