process.py文件
assert group is None, 'group argument must be None for now'
这里又学到了Python的assert断言,判断条件真假,为假的话,就弹出提示
def start(self):
assert self._popen is None, 'cannot start a process twice'
assert self._parent_pid == os.getpid(), \
'can only start a process object created by current process'
assert not _current_process._daemonic, \
'daemonic processes are not allowed to have children'
_cleanup()
if self._Popen is not None:
Popen = self._Popen
else:
from .forking import Popen
self._popen = Popen(self)
_current_process._children.add(self)
(一)开始子进程
popen:打开管道 //待续
os.getpid() 获取现在的进程号
class _MainProcess(Process):
def __init__(self):
self._identity = ()
self._daemonic = False
self._name = 'MainProcess'
self._parent_pid = None
self._popen = None
self._counter = itertools.count(1)
self._children = set()
self._authkey = AuthenticationString(os.urandom(32))
self._tempdir = None
_current_process = _MainProcess()
del _MainProcess
创建一个对象代表主进程
del语句:del 删除引用而不是删除对象,对象由自动垃圾回收机制删除;垃圾回收机制 // 待续
itertools.count(1)
itertools Python自带的迭代函数,会无限的打印自然数序列
next()Python内置函数
def _cleanup():
# check for processes which have finished
for p in list(_current_process._children):
if p._popen.poll() is not None:
_current_process._children.discard(p)
Python进程管理深入解析
本文详细解析了Python中进程管理的实现机制,包括使用assert断言进行条件检查,通过os.getpid()获取当前进程ID,以及如何利用Popen类启动子进程。同时,介绍了_MainProcess类的初始化过程和del语句的用法,以及itertools模块中count函数的应用。
5439

被折叠的 条评论
为什么被折叠?



