python队列
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python队列相关的知识,希望对你有一定的参考价值。
python队列
队列是线程间最常用的数据交换形式,Queue是提供队列的操作模块。三种队列:
1、FIFO
2、LIFO
3、Priority
In [3]: import Queue In [4]: queue= Queue.Queue() In [5]: queue.empty() Out[5]: True In [6]: queue.full() Out[6]: False In [7]: In [7]: In [7]: In [7]: queue= Queue.Queue(5) In [9]: queue.maxsize Out[9]: 5 In [10]: help(Queue.Queue) In [11]: queue.empty() Out[11]: True In [12]: queue.full() Out[12]: False In [13]: queue.maxsize Out[13]: 5 In [14]: queue.put(‘aa‘) In [15]: queue.empty() Out[15]: False In [16]: queue.full() Out[16]: False In [17]: queue.maxsize Out[17]: 5 In [18]: queue.qsize() Out[18]: 1 In [19]: queue.get() Out[19]: ‘aa‘ In [20]: help(queue.put) In [21]: queue.put(1) In [22]: queue.put(2) In [23]: queue.put(3) In [24]: queue.put(4) In [25]: queue.put(5) In [26]: queue.full() Out[26]: True In [27]: queue.put(6,1,3) #保存数据6到队列中,1表示允许阻塞,3表示阻塞3秒后打印报错raise Full --------------------------------------------------------------------------- Full Traceback (most recent call last) <ipython-input-27-0bc7da809a7b> in <module>() ----> 1 queue.put(6,1,3) /opt/amos/python2.7/lib/python2.7/Queue.pyc in put(self, item, block, timeout) 132 remaining = endtime - _time() 133 if remaining <= 0.0: --> 134 raise Full 135 self.not_full.wait(remaining) 136 self._put(item) Full: In [28]: In [28]: In [28]: queue.get() Out[28]: 1 In [29]: queue.get() Out[29]: 2 In [30]: queue.get() Out[30]: 3 In [31]: queue.get() Out[31]: 4 In [32]: queue.get() Out[32]: 5 In [34]: queue.qsize() Out[34]: 0 In [35]: queue.get(1,3) #1表示允许queue的get阻塞,阻塞3秒表示打印错误raise Empty --------------------------------------------------------------------------- Empty Traceback (most recent call last) <ipython-input-35-abf44e15db1f> in <module>() ----> 1 queue.get(1,3) /opt/amos/python2.7/lib/python2.7/Queue.pyc in get(self, block, timeout) 174 remaining = endtime - _time() 175 if remaining <= 0.0: --> 176 raise Empty 177 self.not_empty.wait(remaining) 178 item = self._get()
本文出自 “梅花香自苦寒来!” 博客,请务必保留此出处http://daixuan.blog.51cto.com/5426657/1911434
以上是关于python队列的主要内容,如果未能解决你的问题,请参考以下文章