国产探花免费观看_亚洲丰满少妇自慰呻吟_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 一直在變化。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽宁省| 康保县| 庆云县| 城固县| 大石桥市| 娱乐| 潢川县| 通化县| 乡宁县| 新晃| 崇义县| 抚州市| 岳西县| 隆林| 德清县| 桦甸市| 开阳县| 棋牌| 宜宾市| 五河县| 南木林县| 中西区| 白水县| 内丘县| 顺昌县| 江陵县| 遵义县| 金川县| 保靖县| 莱州市| 宝丰县| 宁波市| 获嘉县| 安福县| 汉川市| 松阳县| 峡江县| 上犹县| 蒙城县| 从化市| 日照市|