python清理子进程机制剖析

yipeiwu_com5年前Python基础

起步

在我的印象中,python的机制会自动清理已经完成任务的子进程的。通过网友的提问,还真看到了僵尸进程。

import multiprocessing as mp
import os
import time
def 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 一直在变化。

相关文章

Python字符串拼接的几种方法整理

Python字符串拼接的几种方法整理

Python字符串拼接的几种方法整理 第一种 通过加号(+)的形式 print('第一种方式通过加号形式连接 :' + 'love'+'Python' + '\n') 第二种 通...

Django应用程序中如何发送电子邮件详解

Django应用程序中如何发送电子邮件详解

前言 在Django应用程序中发送电子邮件最常见的用例是密码重置、帐户激活和发送与您的应用程序相关的一般通知。下面来看看详细的介绍吧。 配置Django发送电子邮件 要配置您的Djang...

Python引用(import)文件夹下的py文件的方法

Python引用(import)文件夹下的py文件的方法

Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行...

深入理解Python变量与常量

深入理解Python变量与常量

变量是计算机内存中的一块区域,变量可以存储规定范围内的值,而且值可以改变。基于变量的数据类型,解释器会分配指定内存,并决定什么数据可以被存储在内存中。常量是一块只读的内存区域,常量一旦被...

python检测lvs real server状态

复制代码 代码如下:import httplibimport osimport time def check_http(i):    try: &...