一:开启进程的方式 第一种
from multiprocessing import Process
import time
def task(name):
print(f"{name} is running ")
time.sleep(3)
print(f"{name} is gone")
if __name__ == '__main__':
p = Process(target = task,args = ('常鑫',))
p.start()
print('===主')
===主
常鑫 is running
常鑫 is gone
开启进程的方式 第二种
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(2)
print(f"{self.name} is gone")
if __name__ == '__main__':
p = MyProcess('常鑫')
p.start()
print('=====主')
'''
=====主
常鑫 is running
常鑫 is gone
'''
开启进程的简单应用
串行的执行结果
from multiprocessing import Process
import time
def task(name):
print(f"{name} is running")
time.sleep(1)
print(f"{name} is gone")
def task1(name):
print(f"{name} is rinning")
time.sleep(2)
print(f"{name} is gone")
def task2(name):
print(f"{name} is running")
time.sleep(3)
print(f"{name} is gone")
if __name__ == '__main__':
p1 = Process(target = task ,args = ('常鑫',))
p2 = Process(target = task,args = ('李业',))
start_time = time.time()
# 一个进程串行的执行三个任务
task('常鑫')
task1('李业')
task2('海狗')
print(f"子进程:{time.time() - start_time}")
''''
常鑫 is running
常鑫 is gone
李业 is rinning
李业 is gone
海狗 is running
海狗 is gone
子进程:6.009300947189331
三个程序并发或者并行的执行三个任务
from multiprocessing import Process
import time
def task(name):
print(f"{name} is running")
time.sleep(1)
print(f"{name} is gone")
def task1(name):
print(f"{name} is rinning")
time.sleep(2)
print(f"{name} is gone")
def task2(name):
print(f"{name} is running")
time.sleep(3)
print(f"{name} is gone")
if __name__ == '__main__':
p1 = Process(target = task ,args = ('常鑫',))
p2 = Process(target = task,args = ('李业',))
start_time = time.time()
p1.start()
p2.start()
task2('海狗')
print(f"结束时间{time.time() - start_time}")
''''
海狗 is running
常鑫 is running
李业 is running
常鑫 is gone
李业 is gone
海狗 is gone
结束时间3.014525890350342
'''
验证昨天的结论 :
- 子进程必须依赖于主进程
- 子进程 copy 了主进程
- 子进程开辟进程,开销比较大,所以开的比较慢
二 : 进程 pid
如何区分内存中的这些进程?
命令获取所有进程的 pid windows 是 tasklist Mac是ps aux
还有一个 ps — ef
代码级别获取 pid
import os
print(os.getpid())
如何获取父进程
import os
print(os.getppid())
三 : 验证进程之间的空间隔离
不可变数据类型的时候
from multiprocessing import Process
import time
import os
name = '太白'
def task():
global name
name = "刚子sb"
print(name)
if __name__ == '__main__':
p = Process(target = task)
p.start()
time.sleep(3) # 睡了一下,所以先执行子进程
print(f"主:{name}")
# 不可变数据类型时,子进程和父进程之间是隔离的,二者毫不影响
# 刚子sb
# 主:太白
我们来看一下可变数据类型
无论子进程中如何操作,在主进程中都是不变的
from multiprocessing import Process
import time
lst = ['丽丽']
def task():
lst.append("怼姐")
print(f"子进程{lst}")
if __name__ == '__main__':
p = Process(target = task)
p.start()
# time.sleep(3)
# 加不加这个结果 完全不同 加了这个先输出子进程
print(f'主{lst}')
子进程['丽丽', '怼姐']
主['丽丽']
总结:无论是可变数据类型,还是不可变数据类型,子进程的操作对主进程都没有影响
四 : join : 让主进程等待子进程执行结束,再执行主进程
这个怎么说呢? join 就是阻塞主进程有 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('===主')
'''
魏无羡 is running
魏无羡 is gone
===主
'''
先执行子进程,然后再执行主进程
多个子进程使用 join
from multiprocessing import Process
import time
def task(name,sec):
print(f"{name} is running")
time.sleep(1)
print(f'{name} is gone')
if __name__ == '__main__':
start_time = time.time()
p1 = Process(target = task,args = ('魏无羡',1))
p2 = Process(target = task,args = ('蓝忘机',2))
p3 = Process(target = task,args = ('蓝曦臣',3))
p1.start()
p2.start()
p3.start()
print(f"主进程:{time.time() - start_time}")
''''
魏无羡 is running
主进程:0.009562253952026367 只是主进程运行的时间
蓝忘机 is running
蓝曦臣 is running
魏无羡 is gone
蓝忘机 is gone
蓝曦臣 is gone
'''
下面的两个验证要好好弄明白
验证一
from multiprocessing import Process
import time
def task(name,sec):
print(f"{name} is running")
time.sleep(1)
print(f'{name} is gone')
if __name__ == '__main__':
start_time = time.time()
p1 = Process(target = task,args = ('魏无羡',1))
p2 = Process(target = task,args = ('蓝忘机',2))
p3 = Process(target = task,args = ('蓝曦臣',3))
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
print(f"主进程:{time.time() - start_time}")
'''
魏无羡 is running
蓝忘机 is running
蓝曦臣 is running
魏无羡 is gone
蓝忘机 is gone
蓝曦臣 is gone
主进程:1.0214881896972656
'''
验证二
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__':
start_time = time.time()
p1 = Process(target=task,args=('常鑫',1))
p2 = Process(target=task,args=('李业',2))
p3 = Process(target=task,args=('海狗',3))
p1.start()
p2.start()
p3.start()
# 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}')
'''
常鑫is running
李业is running
海狗is running
常鑫 is gone
==主1:1.0072300434112549
李业 is gone
===主2:2.0071139335632324
海狗 is gone
==主3:3.0082600116729736
'''
当然还有精简代码
from multiprocessing import Process
import time
def task(sec):
print(f'is running')
time.sleep(sec)
print(f' is gone')
if __name__ == '__main__':
start_time = time.time()
l1 = []
for i in range(1, 4):
p = Process(target=task,args=(i,))
l1.append(p)
p.start()
for i in l1:
i.join()
print(f'==主{time.time()-start_time}')
'''
is running
is running
is running
is gone
is gone
is gone
==主3.013340950012207
'''
五 : 进程的其他参数
terminate 还有p.is_alive( ) 没被杀死返回 True 杀死之后返回 False
执行这个杀死的进程,加睡眠时间 或者是 join 给点缓存时间
args 默认返回值: Process-1
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=('常鑫',),) # 加上返回的就是 alex 不加返回的是Process-1
p.start()
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__':
# 在windows环境下, 开启进程必须在 __name__ == '__main__' 下面
p = Process(target=task,args=('常鑫',)) # 创建一个进程对象
p.daemon = True # 将p子进程设置成守护进程,只要主进程结束,守护进程马上结束.
p.start()
# p.daemon = True # 一定要在子进程开启之前设置
time.sleep(1)
print('===主')
'''
常鑫 is running
===主
'''
原文地址:https://www.cnblogs.com/hualibokeyuan/p/11384612.html
时间: 2024-10-04 19:49:32