开启进程的第一种方式:
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(2)
print(f'{name} is done')
if __name__ == '__main__':
#windows环境,开启进程必须在__name__ == '__main__'下
p = Process(target = task,args = ('alex',))
#创建一个进程对象
p.start()
#只是向操作系统发出一个开辟子进程的信号,然后就执行下一行了
#操作系统收到信号后,会从内存中开辟一个子进程空间,然后再将主进程所有数据copy到子进程,然后再调用cpu去执行(开辟子进程开销很大),所以永远会先执行主进程的代码
print('主进程运行')
#主进程运行
#alex is running
#alex is done
开启进程的第二种方式:
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 done')
if __name__ == '__main__':
p = MyProcess('alex')
p.start()
print('主进程运行')
#主进程运行
#alex is running
#alex is done
简单应用:
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(1)
print(f'{name} is done')
def task1(name):
print(f'{name} is running')
time.sleep(2)
print(f'{name} is done')
if __name__ == '__main__':
p1 = Process(target = task,args = ('alex',))
p2 = Process(target = task,args = ('tony',))
start_time = time.time()
p1.start()
p2.start()
#两个进程,并发或者并行的执行三个任务
print(f'结束时间{time.time() - start_time}')
#结束时间0.03194904327392578
#alex is running
#tony is running
#alex is done
#tony is done
区分内存中的进程:
命令行:获取所有进程的pid(process id): tasklist,
查找某个进程的pid: tasklist|findstr 进程名
代码级别:
import os
print(os.getpid())
获取父进程(主进程的pid):
import os
print(os.getppid())
from multiprocessing import Process
import time
name = 'alex'
def task():
global name
name = 'tom'
print(f'子进程:{name}')
if __name__ == '__main__':
p = Process(target = task)
p.start()
time.sleep(3)
print(f'主进程:{name}')
#子进程:tom
#主进程:alex
lst = [1,2]
def task():
global lst
lst.append(3)
print(f'子进程:{lst}')
if __name__ == '__main__':
p = Process(target = task)
p.start()
time.sleep(3)
print(f'主进程:{lst}')
#子进程:[1, 2, 3]
#主进程:[1, 2]
join:让主进程等待子进程结束之后,再执行主进程
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(2)
print(f'{name} is done')
if __name__ == '__main__':
p = Process(target = task,args = ('alex',))
p.start()
p.join()
print('主进程开始')
#alex is running
#alex is done
#主进程开始
多个子进程使用join,join就是阻塞,主进程有join,主进程下面的代码一律不执行,直到进程执行完毕之后,在执行.
验证1:
from multiprocessing import Process
import time
def task(name,sec):
print(f'{name} is running')
time.sleep(sec)
print(f'{name} is done')
if __name__ == '__main__':
start_time = time.time()
p1 = Process(target = task,args = ('alex',1))
p2 = Process(target = task,args = ('tom',2))
p3 = Process(target = task,args = ('tony,3'))
p1.start()
p2.start()
p3.start()
p1.join() #join只针对主进程,如果join下面多次join,则不阻塞
p2.join()
p3.join()
print(f'主进程:{time.time() - start_time}')
#alex is running
#tom is running
#tony is running
#alex is done
#tom is done
#tony is done
#主进程:3.1952016353607178
验证2:
from multiprocessing import Process
import time
def task(name,sec):
print(f'{name} is running')
time.sleep(sec)
print(f'{name} is done')
if __name__ == '__main__':
start_time = time.time()
p1 = Process(target = task,args = ('alex',1))
p2 = Process(target = task,args = ('tom',2))
p3 = Process(target = task,args = ('tony,3'))
p1.start()
p1.join()
p2.start()
p2.join()
p3.start()
p3.join()
print(f'主进程:{time.time() - start_time}')
#alex is running
#alex is done
#tom is running
#tom is done
#tony is running
#tony is done
#主进程:6.42280125617981
优化上面的代码:
from multiprocessing import Process
import time
def task(name,sec):
print(f'{name} is running')
time.sleep(sec)
print(f'{name} is done')
if __name__ == '__main__':
start_time = time.time()
lst = []
for i in range(1,4):
p = Process(target = task,args = (i,))
lst.append(p)
p.start() #循环时间很快,基本可以实现同时发出指令
for i in lst:
i.join()
print(f'主进程:{time.time() - start_time}')
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(2)
print(f'{name} is done')
if __name__ == '__main__':
p = Process(target = task,args = ('alex',)name = 'tony')
p.start()
p.terminate() #杀死子进程,没有阻塞则子程序还没运行就被杀死
print(P.is_alive()) #看子进程是否还活着,输出True/False
print(p.name) #输出之前给p添加的name属性
print("主进程运行")
守护进程:
子进程守护主进程,只要主进程结束,子进程跟着就结束
from multiprocessing import Process
import time
def task(name):
print(f'{name} is running')
time.sleep(2)
print(f'{name} is done')
if __name__ == '__main__':
p = Process(target = task,args = ('alex',))
p.daemon = True #将子进程p设置成守护进程,主进程结束,子进程也结束
p.start()
#p.daemon = True #一定要在子进程开启之前设置,否则报错
time.sleep(1)
print('主进程运行')
#alex is running
#主进程运行
原文:https://www.cnblogs.com/tutougold/p/11391385.html