#管道Pipe from multiprocessing import Process,Pipe #1 def func1(conn2): msg=conn2.recv() print('conn2:',msg) if __name__ == '__main__': conn1,conn2=Pipe() p=Process(target=func1,args=(conn2,)) p.start() conn1.send('你好') #2 def func1(conn1,conn2): conn1.close() while 1: try: msg=conn2.recv() print('conn2',msg) except EOFError: break if __name__ == '__main__': conn1,conn2=Pipe() p=Process(target=func1,args=(conn1,conn2)) p.start() conn2.close() conn1.send('你好') conn1.close()
#数据共享 Manager from multiprocessing import Process,Manager,Lock #1 def func1(m_dic): m_dic['name']='刘伟' print('子进程结束') if __name__ == '__main__': m=Manager() m_dic=m.dict({'name':'伟'}) p=Process(target=func1,args=(m_dic,)) p.start() print('主进程 :',m_dic) p.join() print('主进程 :', m_dic) #2 def func1(m_dic,lock): with lock:#加锁 m_dic['count']-=1 if __name__ == '__main__': lock=Lock() m_dic=Manager().dict({'count':100}) lst=[] for i in range(20): p=Process(target=func1,args=(m_dic,lock)) p.start() lst.append(p) [el.join() for el in lst] print('主进程m_dic:',m_dic)
#进程池 import time,os from multiprocessing import Process,Pool #1 def func1(n): time.sleep(1) print(n) if __name__ == '__main__': pool=Pool(4) pool.map(func1,range(100)) #2进程池与多进程运行时间对比 def func(n): print(n) if __name__ == '__main__': pool=Pool(4) pool_s=time.time() pool.map(func,range(100))#map异步执行的方法 pool_e=time.time() pool_dif=pool_e-pool_s p_lst=[] p_s=time.time() for i in range(100): p1=Process(target=func,args=(i,)) p1.start() p_lst.append(p1) [p.join() for p in p_lst] p_e=time.time() p_dif=p_e-p_s print('进程池时间:', pool_dif) print('多进程时间:',p_dif) #3apply同步执行的方法 def func(i): time.sleep(0.5) return i**2 if __name__ == '__main__': pool=Pool(4) for i in range(10): ret=pool.apply(func,args=(i,)) print(ret) #4apply_async异步执行的方法 def func(i): time.sleep(2) print(os.getpid()) return i**2 if __name__ == '__main__': pool=Pool(4) lst=[] for i in range(10): ret=pool.apply_async(func,args=(i,)) lst.append(ret) pool.close()#让进程池不再接受别的任务 pool.join()#等待进程池进程全部执行完 for i in lst: print('结果:',i.get())#get取不到会阻塞变为同步,所以用列表先放对象,在循环列表,元素点get取值 #5 pool.close(),pool.join() def func(i): time.sleep(2) print(i) return i**2 if __name__ == '__main__': pool=Pool(4) for i in range(10): ret=pool.apply_async(func,args=(i,)) pool.close()#不允许别的任务在使用进程池,不是关闭进程池 pool.join()#感知进程池中任务的方法 print('主进程结束') #6回调callback def func1(i): print('func_id:',os.getpid()) return i**2 def func2(m): print("func2_id:",os.getpid()) print('func2>>>>',m) # return m**2 if __name__ == '__main__': pool=Pool(4) ret=pool.apply_async(func1,args=(10,),callback=func2) print('ret:',ret.get()) pool.close() pool.join() print('主进程id:',os.getpid())