python 多进程的学习

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)

 

posted @ 2016-03-28 21:43  IDoMyself  阅读(831)  评论(0)    收藏  举报