Multiprocessing & Multithreading
本文主要介绍可以综合使用哪些方法来监测相应子进程/线程的状态,该方法在嵌套循环的场景下尤其重要。
Last Modified: 2022/1/22
Multiprocessing / Multithreading | 子进程/线程状态的监测
示例
此处给出2个示例,分别针对子进程和子线程的状态校验。
子进程
import multiprocessing
import time, os
def Subpro(n):
for i in range(n):
print(multiprocessing.current_process.__name__, 'is going to sleep', i, 's.')
time.sleep(i)
def _get_status(subprocess=None):
'''
dict = {
'initlal': {
subprocess._popen: None,
subprocess.is_alive(): False,
},
'started': {
subprocess._popen: is not None,
subprocess.is_alive(): True,
subprocess._popen.poll: is not None
},
'stopped': {
subprocess._popen: is not None,
subprocess.is_alive(): False,
subprocess._popen.poll: 0 # if exit as expected
},
'closed': {
subprocess._closed: True
}
}
'''
if subprocess._closed:
return 'closed'
if subprocess._popen is None:
if not subprocess.is_alive():
return 'initial'
else:
exitcode = subprocess._popen.poll()
if exitcode is not None:
exitcode = multiprocessing.process._exitcode_to_name.get(exitcode, exitcode)
return 'stopped'
else:
if subprocess._parent_pid != os.getpid():
return 'unknown'
else:
return 'started'
if __name__=='__main__':
pro = multiprocessing.Process(target=Subpro, args=(3, ), name='ChildProcess')
print('initial', _get_status(subprocess=pro))
pro.start()
print('start', _get_status(subprocess=pro))
pro.join()
print('join', _get_status(subprocess=pro))
pro.close()
print('close', _get_status(subprocess=pro))
#########################################################
/Applications/Multiprocessing/Status.py
initial initial
start started
current_process is going to sleep 0 s.
current_process is going to sleep 1 s.
current_process is going to sleep 2 s.
join stopped
close closed
Process finished with exit code 0
子线程
import threading
import time, os
def Subthread(n):
for i in range(n):
print('is going to sleep', i, 's.')
time.sleep(i)
def _get_status(thread=None):
'''
dict = {
'initlal': {
thread.is_alive(): False,
thread._ident: None,# type <class 'NoneType'>
thread._started.is_set(): False,
thread._is_stopped: False
},
'started': {
thread.is_alive(): True,
thread._ident: is not None,
thread._started.is_set(): True,
thread._is_stopped: False
},
'stopped': {
thread.is_alive(): False,
thread._ident: is not None,
thread._started.is_set(): True,
thread._is_stopped: True
}
}
'''
if not thread.is_alive():
if thread._is_stopped:
return 'stopped'
else:
return 'initial'
else:
if thread._started.is_set():
return 'started'
if __name__=='__main__':
subthread = threading.Thread(target=Subthread, args=(3, ), name='Childthread')
print('initial', _get_status(thread=subthread))
subthread.start()
print('start', _get_status(thread=subthread))
subthread.join()
print('join', _get_status(thread=subthread))
#########################################################
/Applications/Multithreading/Status.py
initial initial
is going to sleep 0 s.
start started
is going to sleep 1 s.
is going to sleep 2 s.
join stopped
Process finished with exit code 0
参考链接
写本文时有参考以下链接:
multi-processing-threading/Multiprocessing/Status.py
multi-processing-threading/Multithreading/Status.py
源码&官方doc
cpython/Lib/multiprocessing/
multiprocessing — Process-based parallelism
cpython/Lib/threading.py
threading — Thread-based parallelism