#_author:来童星#date:2019/12/17#多进程队列的使用from multiprocessing import Queueif __name__=='__main__': q=Queue(3)# 初始化一个Queue对象,最多可接收3条put消息 q.put('消息1') q.put('消息2') print(q.full()) q.put('消息3') print(q.full()) try: q.put('消息4',True,2) except: print('消息队列已满,现有消息数量%d'%q.qsize()) try: q.put_nowait() except: print('消息队列已满,现有消息数量%d' % q.qsize()) #读取消息时,先判断消息队列是否为空 if not q.empty(): print('从队列中获取信息') for i in range(q.qsize()): print(q.get_nowait()) #先判断消息队列是否已满,在写入 if not q.full(): q.put_nowait('消息4')运行结果:FalseTrue消息队列已满,现有消息数量3消息队列已满,现有消息数量3从队列中获取信息消息1消息2消息3
来源:https://www.cnblogs.com/startl/p/12054289.html