进程的两种方式
开启进程的第一种方式:
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(1)
print(f'{name} is gone')
if __name__ =='__main__':
p = Process(target = task,args = ('常鑫',))
p.start()(向操作系统发送一个开启子进程的信号,然后执行下一行)
操作系统接收到信号,会在内存中开辟一个子进程的空间,copy主进程中的所有资源加载到子进程空间中,然后调用CPU去执行
开辟子进程开销很大
print('===主')
所以永远会先执行主进程的代码
开辟进程的第二种方式
from multiprocessing import Process
import time
class MyProcess(Process):
def __init__(self,name):
super().__init()
self.name = name
def run(self):
print(f'{self.name} is running')
time.sleep(1)
print(f'{self.name} is gone')
if __name__ =='__main__':
p = MyProcess('zhangsan')
p.start()
print('===主')
简单应用
获取进程pid
from multiprocessing import Process
import time
import os
def task(name):
print(f'{os.getpid()}')
time.sleep(2)
print(f'{os.getppid()}')
if __name__ =="__main__":
p = Process(target=task,args = ('长兴',))
p.start()
print(f'===主{os.getpid()}')
子进程中的主进程id是py文件的子进程id
验证进程之间的空间隔离
from multiprocessing import Process
import time
name = '太白'#li = ['丽丽']
def task():
global name
name = '杆子'
#li.append('刚子')
print(f'子进程:{name/li}')
if __name__ == "__main__":
p = Process(target = task)
p.start()
time.sleep(2)
print(name/li)
join(***)让主进程等待子进程结束之后再执行主进程
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(2)
print(f'{name} is gone')
if __name__ == '__main__':
p = Process(target = task,args = ('长兴',))
p.start()
p.join()
print('==主开始')
from multiprocessing import Process
import time
def task(name,sec):
print(f'{name} is running')
time.sleep(sec)
print(f'{name} is gone')
if __name__ == '__main__':
p1 = Process(target = task,args = ('长兴',1))
p2 = Process(target = task,args = ('立业',2))
p3 = Process(target = task,args = ('海狗',3))
p1.start()
p2.satrt()
p3.start()
#join 只针对主进程,如果join下面多次join,他是不阻塞的
p1.join()
p2.join()
p3.join()
print(f'==主{time.time() - start_time}')
from multiprocessing import Process
import time
def task(name,sec):
print(f'{name} is running')
time.sleep(sec)
print(f'{name} is gone')
if __name__ == '__main__':
p1 = Process(target = task,args = ('长兴',1))
p2 = Process(target = task,args = ('立业',2))
p3 = Process(target = task,args = ('海狗',3))
p1.start()
p2.satrt()
p3.start()
#join 只针对主进程,如果join下面多次join,他是不阻塞的
p1.join()
print(f'==主1:{time.time()-start_time()}')
p2.join()
print(f'==主2:{time.time()-start_time()}')
p3.join()
print(f'==主3:{time.time() - start_time}')
from multiprocessing import Process
import time
def task(sec):
print(f'is running')
time.sleep(sec)
print(f'is gone')
if __name__ =='__main__':
p1 = Process(target = task,args = ('长兴',1))
p2 = Process(target = task,args = ('立业',2))
p3 = Process(target = task,args = ('海狗',3))
p1.start()
p2.satrt()
p3.start()
#join 只针对主进程,如果join下面多次join,他是不阻塞的
p1.join()
p2.join()
p3.join()
li =[]
for i in range(1,4):
p = Process(target = task,args = (i,))
li.append(p)
p.start()
for i in li:
i.join()
print(f'==主:{time.time()-start_time}')
join就是阻塞,主进程有join,主进程下面的代码一律不执行,直到进程执行完毕之后,再执行
进程的其他参数
terminate()杀死子进程
is_alive()判断子进程是否存活
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(2)
print(f'{name} is gone')
if __name__ == '__main__':
# p = Process(target=task,args=('长兴',))
p = Process(target=task,args=('长兴',),name = 'alex')
p.start()
time.sleep(1)
p.terminate()#杀死子进程
p.join()
time.sleep(0.5)
print(p.is_alive())
print(p.name)
p.name = 'sb'
print(p.name)
print('==主开始')
守护进程:子进程守护着主进程,只要主进程结束,子进程跟着就结束
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(2)
print(f'{name} is gone')
if __name__ == '__main__':
p = Process(target=task,args = ('常鑫',))
p.daemon = True#讲子进程设置为守护进程,只要主进程结束,守护进程马上就会结束
一定要在子进程开始之前设置
p.start()
time.sleep(1)
print('===主')
原文:https://www.cnblogs.com/-777/p/11385867.html