"""自定义进程子类"""
from multiprocessing import Process, current_process
import time
class MyProceee(Process):
def __init__(self):
super().__init__()
def run(self):
print("必须重写进程类的run方法")
print("调用start方法时,会执行这个重写的run方法")
print(current_process().pid, current_process())
time.sleep(1)
if __name__ == '__main__':
p_list = []
for i in range(20):
p = MyProceee()
p.start()
p_list.append(p)
if p.is_alive(): # 进程是否存活
p.terminate() # 结束进程
time.sleep(0.5)
print(p.is_alive())
for p in p_list:
p.join()
from multiprocessing import Process, current_process
import time
class MyProceee(Process):
def __init__(self):
super().__init__()
def run(self):
print("必须重写进程类的run方法")
print("调用start方法时,会执行这个重写的run方法")
print(current_process().pid, current_process())
time.sleep(1)
if __name__ == '__main__':
p_list = []
for i in range(20):
p = MyProceee()
p.start()
p_list.append(p)
for p in p_list:
p.join()