多进程队列的使用

#_author:来童星
#date:2019/12/17
#多进程队列的使用
from multiprocessing import Queue
if __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')
运行结果:
False
True
消息队列已满,现有消息数量3
消息队列已满,现有消息数量3
从队列中获取信息
消息1
消息2
消息3

猜你喜欢

转载自www.cnblogs.com/startl/p/12054289.html