1、多进程Process
调用方法:
from multiprocessing import Process,freeze_support import time def f(i): time.sleep(1) print(i) # 这里如果不写if __name__ == "__main__":的话会报freeze_support()错误 if __name__ == "__main__": for i in range(10): p = Process(target=f,args=(i,)) p.start() print("end")
2、队列Queue
Queue为先进先出,get()一次,Queue里的那个被get的值会被拿走,当队列为空时get()方法会阻塞,
调用方法:
from multiprocessing import Process,Queue import time def f(i,q): time.sleep(1) q.put(i) if __name__ == "__main__": q = Queue() for i in range(2): p = Process(target=f,args=(i,q,)) p.start() for i in range(2): print(q.get())
3、Pipe
示例: from multiprocessing import Process,Queue,Pipe import time def f(i,conn): conn.send(i*10) if __name__ == "__main__": parent, child = Pipe() for i in range(2): p = Process(target=f,args=(i,child)) p.start() for i in range(2): print(parent.recv())
4、Manager,进程间数据共享
如果有子进程在使用Manage()对象时,在父进程不能使用这个对象,所以要等所有子进程结束即需使用p.join()后方可在父进程使用Manage()的对象。
如果这里注释join()会报:FileNotFoundError: [WinError 2] 系统找不到指定的文件。
from multiprocessing import Process,Manager import time def f(d,l,i): time.sleep(5-i) # 最后起的进程睡眠最少时间,所以父进程不能简单的使用p.join(),而是要把p加入p_list中,然后循环p_list,并使用join()方法 d[i] = i*10 l.append(i*100) if __name__ == "__main__": with Manager() as m: d = m.dict() l = m.list([1,2,3]) p_list = [] for i in range(5): p = Process(target=f,args=(d,l,i,)) p.start() p_list.append(p) for p in p_list: p.join() print("d:",d) print("l:",l)
5、进程池Pool
Pool满时后面的进程会被挂起,当pool中一个进程结束时,挂起的进程会马上开始执行。
from multiprocessing import Process,Pool import time def f(i): print("begin:",i,time.time()) time.sleep(i) print("end:",i,time.time()) if __name__ == "__main__": pool = Pool(5) for i in range(1,10): pool.apply_async(func=f,args=(i,)) pool.close() pool.join() 执行结果: begin: 1 1466257881.265401 begin: 2 1466257881.2779033 begin: 3 1466257881.2944064 begin: 4 1466257881.2968454 begin: 5 1466257881.3089068 end: 1 1466257882.265722 begin: 6 1466257882.265722 end: 2 1466257883.2781172 begin: 7 1466257883.2781172 end: 3 1466257884.294556 begin: 8 1466257884.295386 end: 4 1466257885.2975235 begin: 9 1466257885.2975235 end: 5 1466257886.3092482 end: 6 1466257888.2660825 end: 7 1466257890.2784016 end: 8 1466257892.295543 end: 9 1466257894.2983513
时间: 2024-10-25 00:53:39