国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python清理子進程機制剖析

2020-02-16 10:50:33
字體:
來源:轉載
供稿:網友

起步

在我的印象中,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 一直在變化。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 汝阳县| 武功县| 乌拉特中旗| 甘肃省| 庆安县| 腾冲县| 广州市| 五原县| 诏安县| 灵寿县| 宁海县| 安图县| 灌阳县| 寿宁县| 翼城县| 班玛县| 惠州市| 郴州市| 宁城县| 苗栗县| 永城市| 安龙县| 勃利县| 清流县| 东平县| 瑞丽市| 兴隆县| 南康市| 南昌市| 巴楚县| 阿合奇县| 集贤县| 丹寨县| 开原市| 平山县| 马山县| 华容县| 哈巴河县| 库尔勒市| 北川| 温州市|