进程间的通信
1 import multiprocessing 2 import time 3 4 def write_quene(quene): 5 for i in range(10): 6 if quene.full(): 7 print("quene is full") 8 break 9 quene.put(i) 10 print("input successfully:",i) 11 12 def read_quene(quene): 13 while True: 14 if quene.empty(): 15 print("The quene is empty,can not output") 16 break 17 18 value = quene.get() 19 print(value) 20 21 if __name__ == ‘__main__‘: 22 quene = multiprocessing.Queue() 23 write_process = multiprocessing.Process(target=write_quene,args=(quene,)) 24 read_process = multiprocessing.Process(target=read_quene,args=(quene,)) 25 26 write_process.start() 27 # 由于两个进程同时进行读入和取出会造成预取之外的效果,因此用join先执行一个进程 28 write_process.join() 29 read_process.start()
原文地址:https://www.cnblogs.com/liuxjie/p/11984918.html
时间: 2024-10-21 03:58:53