标签:队列 queue def code join pen col 进程 empty
进程间的通信方式有 管道,消息队列 ,共享内存,信号(不太会),套接字(用于web)
管道方式:一个放入一个取出
def ps(c,msg): print("child start") c.send("hello"+str(msg)) print(os.getpid()) if __name__=="__main__": c,pa=Pipe() li=[] for i in range(5): p=Process(target=ps,args=(c,"王")) li.append(p) p.start() for i in range(5): data=pa.recv() print(data) for i in range(5): p.join()
消息队列:先放入消息再取出消息
def ps(c,msg): c.put("hello"+str(msg)) if __name__=="__main__": q=Queue() l=[] for i in range(5): p=Process(target=ps,args=(q,"_--")) l.append(p) p.start() for i in l: i.join() while not q.empty(): print(q.get())
共享内存:
def ps(msg): for i in range(5): if msg.value==100: msg.value=0 else: msg.value=100 print(msg.value) sleep(1) if __name__=="__main__": msg=Value("i",100) p1=Process(target=one,args=(msg,)) p2=Process(target=one,args=(msg,)) p1.start() p2.start() p1.join() p2.join()
标签:队列 queue def code join pen col 进程 empty
原文地址:https://www.cnblogs.com/wang14339/p/9469606.html