1.开启子进程的两种方式:
1 # 方式1: 2 from multiprocessing import Process 3 import time 4 5 def task(name): 6 print('%s is running'%(name)) 7 time.sleep(2) 8 print('%s is done'%name) 9 10 if __name__ == '__main__': 11 # p = Process(target=task,kwargs={'name':'子进程1'}) 12 p = Process(target=task,args=('子进程1',)) 13 p.start() # 仅仅只是给os发了一个信号 14 15 print('主') 16 ''' 17 主 18 子进程1 is running 19 子进程1 is done 20 ''' 21 22 # 方式二: 23 from multiprocessing import Process 24 import time 25 26 class MyProcess(Process): 27 def __init__(self,name): 28 super().__init__() 29 self.name = name 30 31 def run(self): # 只能叫run 32 print('%s is running'%(self.name)) 33 time.sleep(2) 34 print('%s is done'%self.name) 35 36 if __name__ == '__main__': 37 p = MyProcess('子进程1') 38 p.start() 39 40 print('主') 41 42 ''' 43 主 44 子进程1 is running 45 子进程1 is done 46 '''
2.查看pid:
1 from multiprocessing import Process 2 import time 3 import os 4 5 6 def task(name): 7 print('%s is running'%(name),os.getpid(),'parent:',os.getppid()) 8 time.sleep(2) 9 print('%s is done'%name,os.getpid(),'parent:',os.getppid()) 10 11 if __name__ == '__main__': 12 # p = Process(target=task,kwargs={'name':'子进程1'}) 13 p = Process(target=task,args=('子进程1',)) 14 p.start() # 仅仅只是给os发了一个信号 15 16 print('主',os.getpid(),'parent:',os.getppid()) 17 18 ''' 19 主 53400 parent: 12480 # pycharm的pid 20 子进程1 is running 51432 parent: 53400 21 子进程1 is done 51432 parent: 53400 22 ''' 23 ''' 24 cmd: 25 tasklist | findstr pycharm 26 pycharm64.exe 12480 Console 1 776,848 K 27 ''' 28 ''' 29 linux: 30 ps aux | grep pycharm 31 bj 30819 0.0 0.0 21312 976 pts/1 R+ 12:57 0:00 grep --color=auto pycharm 32 ''' 33 34 ''' 35 僵尸进程与孤儿进程: 36 如果父进程一直不死,子进程变成了僵尸进程后一直占着pid ,有害! 37 父进程死之前,要wait()...回收僵尸进程的pid 38 孤儿进程,父进程先死了,linux中最大的父进程init进程会回收孤儿进程的pid 39 孤儿进程无害!最后init进程会回收! 40 如果父进程不死,僵尸进程有害,一直占着pid 41 42 '''
3.Process对象的其他属性或方法:
1 # 1个子进程 join 方法: 2 from multiprocessing import Process 3 import time 4 import os 5 6 7 def task(name): 8 print('%s is running'%(name),os.getpid(),'parent:',os.getppid()) 9 time.sleep(2) 10 print('%s is done'%name,os.getpid(),'parent:',os.getppid()) 11 12 if __name__ == '__main__': 13 p = Process(target=task,args=('子进程1',)) 14 p.start() # 仅仅只是给os发了一个信号 15 16 p.join() # 主进程等待子进程的运行完成 17 print('主',os.getpid(),'parent:',os.getppid()) 18 print(p.pid) # 子进程结束了,但是变成了僵尸进程,子进程的pid 还存在 19 """ 20 子进程1 is running 43680 parent: 46512 21 子进程1 is done 43680 parent: 46512 22 主 46512 parent: 12480 23 43680 24 """ 25 """ 26 主进程结束了,基于它的子进程才会结束,即僵尸进程的pid才会被回收! 27 """ 28 29 # 多个子进程join方法 并行 30 from multiprocessing import Process 31 import time 32 import os 33 34 def task(name,n): 35 print('%s is running'%(name)) 36 time.sleep(n) 37 38 if __name__ == '__main__': 39 start = time.time() 40 p1 = Process(target=task,args=('子进程1',5)) 41 p2 = Process(target=task,args=('子进程2',3)) 42 p3 = Process(target=task,args=('子进程3',2)) 43 p_l = [p1,p2,p3] 44 # p1.start() # 仅仅只是给os发了一个信号 45 # p2.start() 46 # p3.start() 47 for p in p_l: 48 p.start() 49 50 # p1.join() # 主进程在等... 等的是最长的那个时间 5 3 2 51 # p2.join() # 这里任然是并发 52 # p3.join() 53 for p in p_l: 54 p.join() 55 56 print('主',(time.time()-start)) 57 ''' 58 主 34504 parent: 12480 59 子进程1 is running 51732 parent: 34504 60 子进程3 is running 48864 parent: 34504 61 子进程2 is running 16240 parent: 34504 62 ''' 63 ''' 64 子进程3 is running 46212 parent: 35732 65 子进程1 is running 1528 parent: 35732 66 子进程2 is running 50244 parent: 35732 67 主 35732 parent: 12480 68 ''' 69 """ 70 子进程1 is running 71 子进程2 is running 72 子进程3 is running 73 主 5.094129323959351 # 等的是5秒 任然是并发,不是串行。 74 """ 75 76 # 多个子进程join方法 串行 77 from multiprocessing import Process 78 import time 79 import os 80 81 def task(name,n): 82 print('%s is running'%(name)) 83 time.sleep(n) 84 85 if __name__ == '__main__': 86 start = time.time() 87 p1 = Process(target=task,args=('子进程1',5)) 88 p2 = Process(target=task,args=('子进程2',3)) 89 p3 = Process(target=task,args=('子进程3',2)) 90 91 p1.start() 92 p1.join() 93 p2.start() 94 p2.join() 95 p3.start() 96 p3.join() 97 98 print('主',(time.time()-start)) # 这个时候就是 串行 99 """ 100 子进程1 is running 101 子进程2 is running 102 子进程3 is running 103 主 10.235251665115356 104 """ 105 106 # 其他方法:is_alive() terminate() pid name 107 from multiprocessing import Process 108 import time 109 import os 110 111 112 def task(name): 113 print('%s is running'%(name),os.getpid()) 114 time.sleep(2) 115 116 if __name__ == '__main__': 117 # p = Process(target=task,args=('子进程1',)) 118 # p.start() 119 # print(p.is_alive()) 120 # p.join() 121 # print('主') 122 # print(p.pid) # 子进程即僵尸进程pid 123 # print(p.is_alive()) # 查看进程是否还活着 124 125 p = Process(target=task,name='sub-process',args=('子进程1',)) 126 p.start() 127 # p.terminate() # 给os 发了个信号,还没来的及杀死子进程 128 # time.sleep(2) 129 # print(p.is_alive()) # True --> 加了time.sleep(2) --> False 130 print('主') 131 132 print(p.name,p.pid) 133 """ 134 主 135 Process-1 48456 136 子进程1 is running 48456 137 """ 138 """ 139 主 140 sub-process 43556 141 子进程1 is running 43556 142 """
4.守护进程
1 """ 2 开进程的目的:并发任务;假设任务在主进程死后没意义存在了,就设为守护进程。 3 守护进程: 4 0.p.daemon = True 5 1.主进程结束时,守护进程就完了 6 2.守护进程一定要在子进程start()前设置 7 3.不允许在守护进程内在开子进程 8 """ 9 from multiprocessing import Process 10 import time 11 12 def task(name): 13 print('%s is running'%name) 14 time.sleep(2) 15 # p = Process(target=time.sleep,args=(3,)) 16 # p.start() 17 18 19 if __name__ == "__main__": 20 p = Process(target=task,args=('子进程1',)) 21 p.daemon = True # 守护进程 主结束时,子进程就完了 ,守护进程一定要在子进程start() 前设置 22 p.start() 23 p.join() 24 print('主') 25 """ 26 主 27 """ 28 """ 29 子进程1 is running 30 主 31 """ 32 33 """练习题:""" 34 # 思考下列代码的执行结果有可能有哪些情况?为什么? 35 from multiprocessing import Process 36 import time 37 38 def foo(): 39 print(123) 40 time.sleep(1) 41 print("end123") 42 43 def bar(): 44 print(456) 45 time.sleep(3) 46 print("end456") 47 48 if __name__ == '__main__': 49 p1=Process(target=foo) 50 p2=Process(target=bar) 51 52 p1.daemon=True # 主进程死后,代码执行完毕,守护进程就死了 53 p1.start() 54 p2.start() 55 print("main-------") # 只要这里一运行,守护进程就完了 56 57 """ 58 main------- 59 456 60 end456 61 """