初识进程池
场景一:主进程不会等进程池内部的任务执行完毕后再执行,而是直接执行,进程池内部的任务,自己处理
import os,time
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
def task(num):
print("子进程的pid:",os.getpid())
print("执行:",num)
time.sleep(2)
if __name__ == '__main__':
#创建一个进程池,进程池里四个进程
pool = ProcessPoolExecutor(4)
for i in range(10):
pool.submit(task,i)
#打印abc,不会等待进程池里的内容执行完,而是直接执行
print("a")
print("b")
print("c")
image.png
场景二:200个任务交给进程池去处理,进程池内部的任务处理完成后,再执行某些功能,需要人为做一些阻塞的操作。
import os,time
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
def task(num):
print("子进程的pid:",os.getpid())
print("执行:",num)
time.sleep(2)
if __name__ == '__main__':
#创建一个进程池,进程池里四个进程
pool = ProcessPoolExecutor(4)
for i in range(10):
pool.submit(task,i)
#等待进程池中的任务都执行完毕后,再继续完后执行
pool.shutdown(True)
print("a")
print("b")
print("c")
image.png
场景三:进程池里拿了一个进程,进行操作,操作结束后,还想做另外的操作,加回调函数的功能。
import os,time
import multiprocessing
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
def task(num):、
print("子任务:",multiiprocessing.current_process().pid)
print("执行:",num)
time.sleep(2)
return num
def done(res):
print("done函数里的的current_process:",multiprocessing.current_process())
time.sleep(1)
print("done函数里获取的返回结果:",res.result())
time.sleep(1)
if __name__ == '__main__':
#创建一个进程池,进程池里四个进程
pool = ProcessPoolExecutor(4)
for i in range(10):
fur = pool.submit(task,i)
fur.add_done_callback(done) #done的调用由主进程处理(与线程池不同)
print("主进程的current_process:", multiprocessing.current_process())
#等待进程池中的任务都执行完毕后,再继续完后执行
pool.shutdown(True)
print("a")
print("b")
print("c")
image.png
image.png
进程池+进程锁
import os,time
import multiprocessing
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
def task(lock):
print("开始")
#假设文件中保存的内容就是一个值:10
with open ("f1.txt",mode = "r",encoding = "utf-8") as f:
current_num = int(f.read())
print("排队抢票了")
time.sleep(0.5)
current_num -= 1
with open("f1.txt",mode = "w",encoding = "utf-8") as f:
f.write(str(current_num))
if __name__ == '__main__':
pool = ProcessPoolExecutor()
manager = multiprocessing.Manager()
lock_object = manager.RLock()
for i in range(10):
pool.submit(task,lock_object)
网友评论