pythonQueue在python的实例化
Queue在python的实例化
1、Queue的方法
实例化Queue(),未指定可接收信息的参数或该参数为负数,则默认无上限(内存上限)
Queue.qsize():返回当前队列包含消息的数量
Queue.empty():判断队列是否为空,是为True,否为False
Queue.full():判断队列是否满了,是为True,否为False
Queue.get([block[,timeout]]):获取一条队列的消息,然后将其从队列中删除,black默认为True
如果block为默认值,没有设置timeout,消息队列又为空,那么此时程序将会阻塞(停在读取状态),直到队列中有值,从消息队列中读取到值.如果设置了timeout,等待timeout的时间就会报出(Queue.Empty)异常
如果block为False,消息队列为空,则会立刻抛出(Queue.Empty)异常
2、实例化
frommultiprocessingimportQueue
q=Queue(3)#在实例化时传入消息队列的数量
q.put("test1")
q.put("test2")
print(q.full())#这里队列未满,返回False
q.put("test3")
print(q.full())#这里队列已经满了,返回True
try:
q.put("test4",timeout=2)#在写入时,如果队列是满的,就等待两秒钟,如还不可以,抛出异常
except:
print("当前消息队列的数量为%s"%q.qsize())
try:
q.put("test4",block=False)#在写入时,如果队列是满的,直接抛出异常
except:
print("当前消息队列的数量为%s"%q.qsize())
#在写入之前,可以判断下队列是否是满的
ifnotq.full():
q.put_nowait("test4")
#同理,在取数之前,判断队列是否为空
ifnotq.empty():
foriinrange(q.qsize()):
print(q.get_nowait())
以上就是Queue在python实例化的方法,希望能对大家有所帮助,更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
Python如何在画布中绘制多个图表?
python如何在画布中绘制多个图表?1、说明在绘制图表时,Matplotlib模块默认先创建一块画布,然后将绘制的图表显示在画布上。若要在一个画布上画...详情>>
2023-11-14 21:04:27
python@是什么意思
python中@是什么意思通常我们所见的@符号是用于邮箱中,而在python中@符号也有着重要的作用,一个是表示修饰符,另一个则是表示矩阵乘法。表示...详情>>
2023-11-14 20:07:01
python进程池的使用注意
python进程池的使用注意1、使用注意(1)使用池时,如果未指定进程数,则默认为CPU核心数。(2)核心数量对应于计算机的逻辑处理器数量(任务管理器-...详情>>
2023-11-14 18:42:08
python多线程中join的应用场景
python多线程中join的应用场景1、线程的join()方法表示等这个线程运行完毕,程序再往下运行。我们来看下面的例子fromthreadingimportThreadt=ti...详情>>
2023-11-14 18:16:56热门推荐
python中scipy.linalg.inv()功能是什么?
沸requests在python中如何发送请求
热python自定义模块的注意点
热python如何将相对路径转换为绝对路径?
新python在列表中和列表末尾添加元素的方法
Python如何在画布中绘制多个图表?
pythonQueue在python的实例化
python@是什么意思
pythonconftest如何使用?
python列表运算详解
python进程池的使用注意
python多线程中join的应用场景
python异步中loop抛出异常的解决
pythontime模块处理系统时间的函数
技术干货






