起步
在我的印象中,python的機制會自動清理已經完成任務的子進程的。通過網友的提問,還真看到了僵尸進程。
import multiprocessing as mpimport osimport timedef pro(): print ("os.pid is ", os.getpid())if __name__ == '__main__': print ("parent ", os.getpid()) while True: p = mp.Process(target = pro) p.start() time.sleep(1)
于是我覺得我要重新了解一下這個過程。
銷毀僵尸進程的時機
mutilprossing.Process 繼承自 BaseProcess 文件在 Lib/mutilprossing/process.py 中,我們看看它的start方法:
_children = set()class BaseProcess(object): def start(self): self._check_closed() _cleanup() self._popen = self._Popen(self) self._sentinel = self._popen.sentinel # Avoid a refcycle if the target function holds an indirect # reference to the process object (see bpo-30775) del self._target, self._args, self._kwargs _children.add(self)
_children 是一個全局的集合變量,保存著所有 BaseProcess 實例, start 函數末尾處 _children.add(self) 將進程對象放入。又注意到 _cleanup() 函數:
def _cleanup(): # check for processes which have finished for p in list(_children): if p._popen.poll() is not None: _children.discard(p)
_popen 是一個 Popen 對象,代碼在 multiprossing/popen_fork.py 中,其 poll 函數有個 id, sts = os.waitpid(self.pid, flag) 一個回收子進程的函數。回收后再將 BaseProcess 子類實例從_children中移除。
這下就清楚了,python在子進程start中將進程放入集合,子進程可能長時間運行,因此這個集合上的進程會有很多狀態,而為了防止過多僵尸進程導致資源占用,python會在下一個子進程 start 時清理僵尸進程。所以,最后一個子進程在自身程序運行完畢后就變成僵尸進程,它在等待下一個子進程start時被清理。所以 ps 上總有一個僵尸進程,但這個僵尸進程的 進程id 一直在變化。
新聞熱點
疑難解答