from multiprocessing import Process, Queue import time,os def w_fun(qunue_p): print('写进程号', os.getpid()) time.sleep(2) for i in range(11): if qunue_p.full(): print('队列已满,写进程正在等待。。。') # time.sleep(2) qunue_p.put(i) # qunue_p.put(11) # qunue_p.put(12) # qunue_p.put(13) # qunue_p.put(14) # qunue_p.put(15) # # 阻塞 # qunue_p.put(16) def do_fun(obj_p): print('进程号', os.getpid()) print('处理中。。。', obj_p) def r_fun(qunue_p): # task1 = qunue_p.get() # task2 = qunue_p.get() # task3 = qunue_p.get() # task4 = qunue_p.get() # task5 = qunue_p.get() # # task6 = qunue_p.get() # print(task1, task2, task3, task4, task5) print('读进程号', os.getpid()) for i in range(11): if qunue_p.qsize() == 0: print('读进程陷入阻塞,请等待。。。') t = qunue_p.get() print('读到任务--->', t) # 穿件处理任务的进程 pdo = Process(target=do_fun, args=(t,)) pdo.start() pdo.join() def main(): q = Queue(10) pw = Process(target=w_fun, args=(q,)) pw.start() # pw.join() pr = Process(target=r_fun, args=(q,)) print('朱建成号', os.getpid()) pr.start() pw.join() print('写进程结束!!!') pr.join() print('读进程结束!!!') print('finish') if __name__ == '__main__': main()
队列通信
最新推荐文章于 2023-06-18 20:25:58 发布