日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Queue模块

發布時間:2025/4/9 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Queue模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
隊列是線程間最常用的數據交換的形式 Queue是提供隊列操作的模塊 Queue提供了三種隊列操作:
  • FIFO(first in first out 先進先出):Queue.Queue(maxsize=0)
  • LIFO(last in first out 后進先出):Queue.LifoQueue(maxsize=0)
  • Priority(優先級隊列):Queue.PriorityQueue(maxsize=0)
  • FIFO(先進先出)
    • 1.1 Queue.Queue這個類的介紹
    In [13]: import QueueIn [14]: help(Queue.Queue) Help on class Queue in module Queue:class Queue| Create a queue object with a given maximum size. //創建一個隊列對象,隊列的最大長度是maxsize的值| | If maxsize is <= 0, the queue size is infinite. //如果最大長度<=0 隊列的長度不受限制| | Methods defined here:| | __init__(self, maxsize=0) //構造函數接受一個參數maxsize,該參數定義了隊列的長度|.........In [15]: queue = Queue.Queue(5) //實例化一個長度為5的對象隊列

    ?

    對象的方法:queue.empty()

    In [15]: queue = Queue.Queue(5)In [17]: help(queue.empty) Help on method empty in module Queue:empty(self) method of Queue.Queue instanceReturn True if the queue is empty, False otherwise (not reliable!). //如果隊列為空,返回True; 否則返回FalseIn [16]: queue.empty() Out[16]: True

    ?

    對象的方法:queue.full()

    In [15]: queue = Queue.Queue(5)In [18]: help(queue.full) Help on method full in module Queue:full(self) method of Queue.Queue instanceReturn True if the queue is full, False otherwise (not reliable!). //如果隊列已滿,返回True; 否則返回FalseIn [19]: queue.full() Out[19]: False

    ?

    對象的方法:queue.maxsize()

    In [23]: queue = Queue.Queue(5)In [24]: queue.maxsize //查看隊列的長度 Out[24]: 5

    ?

    隊列的操作:queue.put()

    In [25]: help(queue.put) Help on method put in module Queue:put(self, item, block=True, timeout=None) method of Queue.Queue instancePut an item into the queue. //往隊列存入數據If optional args 'block' is true and 'timeout' is None (the default), //if block=True and timeout=None,當隊列滿了,這時再往隊列添加數據block if necessary until a free slot is available. If 'timeout' is //隊列會處于一直阻塞狀態,直到數據被取走有新位置存入,如果timeout是一個正整數a positive number, it blocks at most 'timeout' seconds and raises //隊列會阻塞timeout seconds,如果期間仍然沒有位置存數據,timeout完后會拋出the Full exception if no free slot was available within that time. //Full exceptionOtherwise ('block' is false), put an item on the queue if a free slotis immediately available, else raise the Full exception ('timeout' //如果block=False,那么timeout參數不會生效,當隊列已滿然而沒有立即空出位置is ignored in that case). //會馬上拋出Full exceptionIn [26]: queue.empty() Out[26]: TrueIn [27]: queue.maxsize Out[27]: 5In [28]: queue.put(1)In [29]: queue.put(2)In [30]: queue.put(3)In [31]: queue.put(4)In [32]: queue.put(5)In [33]: queue.full() Out[33]: TrueIn [34]: queue.put(6, 1, 3) //1代表block=True; timeout=3 3秒后還是阻塞狀態的話raise --------------------------------------------------------------------------- Full Traceback (most recent call last) <ipython-input-34-adf947887ccb> in <module>() ----> 1 queue.put(6, 1, 3)/usr/lib64/python2.6/Queue.pyc in put(self, item, block, timeout)132 remaining = endtime - _time()133 if remaining <= 0.0: --> 134 raise Full135 self.not_full.wait(remaining)136 self._put(item)Full:In [36]: queue.put(6, 0) //0表示block=False,如果隊列已滿并且沒有立馬空出位置,馬上raise --------------------------------------------------------------------------- Full Traceback (most recent call last) <ipython-input-36-d306fb36c223> in <module>() ----> 1 queue.put(6, 0)/usr/lib64/python2.6/Queue.pyc in put(self, item, block, timeout)121 if not block:122 if self._qsize() == self.maxsize: --> 123 raise Full124 elif timeout is None:125 while self._qsize() == self.maxsize:Full:

    ?

    隊列的操作:queue.get()

    In [37]: help(queue.get) Help on method get in module Queue:get(self, block=True, timeout=None) method of Queue.Queue instanceRemove and return an item from the queue. //從隊列里刪除(取走)數據并返回該數據If optional args 'block' is true and 'timeout' is None (the default), //if block=True and timeout=None,隊列會一直阻塞直到有數據存入隊列block if necessary until an item is available. If 'timeout' is //如果timeout是個正整數,如果沒有數據存入而當前隊列為空,那么timeout完后會拋出a positive number, it blocks at most 'timeout' seconds and raises //Empty exceptionthe Empty exception if no item was available within that time.Otherwise ('block' is false), return an item if one is immediately //如果block=False,那么timeout參數不會生效,如果沒有數據存入而當前隊列為空available, else raise the Empty exception ('timeout' is ignored //馬上拋出Empty exceptionin that case).In [41]: queue.full() Out[41]: TrueIn [42]: queue.qsize() Out[42]: 5In [43]: queue.get() Out[43]: 1In [44]: queue.get() Out[44]: 2In [45]: queue.get() Out[45]: 3In [46]: queue.get() Out[46]: 4In [47]: queue.get() Out[47]: 5In [48]: queue.empty() Out[48]: TrueIn [49]: queue.get(1, 3) //1表示block=True;3表示timeout=3; 如果3秒內沒有數據存入而當前隊列為空,那么3秒后raise --------------------------------------------------------------------------- Empty Traceback (most recent call last) <ipython-input-49-84d017e05bdb> in <module>() ----> 1 queue.get(1, 3)/usr/lib64/python2.6/Queue.pyc in get(self, block, timeout)174 remaining = endtime - _time()175 if remaining <= 0.0: --> 176 raise Empty177 self.not_empty.wait(remaining)178 item = self._get()Empty:In [50]: queue.get(0) //0表示block=False;當前隊列為空,且沒有立即有數據存入,馬上raise --------------------------------------------------------------------------- Empty Traceback (most recent call last) <ipython-input-50-c4ae3bdc3aa5> in <module>() ----> 1 queue.get(0)/usr/lib64/python2.6/Queue.pyc in get(self, block, timeout)163 if not block:164 if not self._qsize(): --> 165 raise Empty166 elif timeout is None:167 while not self._qsize():Empty:

    ?

    對象的方法:queue.qsize()

    In [51]: help(queue.qsize) Help on method qsize in module Queue:qsize(self) method of Queue.Queue instanceReturn the approximate size of the queue (not reliable!). //返回當前隊列的長度In [52]: queue.put('aa')In [53]: queue.put('1')In [54]: queue.put(2) In [55]: queue.get() Out[55]: 'aa'In [56]: queue.qsize() Out[56]: 2

    ?

    ?

    例子:

    #!/usr/bin/env pythonimport threading import random import time import Queueclass Producer(threading.Thread):def __init__(self, queue):threading.Thread.__init__(self)self.queue = queueself.new_name = 'producer_' + self.namedef run(self):for i in xrange(10):num = random.randint(1, 10)print '%s: %s ---> %s' % (time.ctime(), self.new_name, num)self.queue.put(num)time.sleep(1)class Odd(threading.Thread):def __init__(self, queue):threading.Thread.__init__(self)self.queue = queueself.new_name = 'odd_' + self.namedef run(self):while True:try:num = self.queue.get(1, 5)if num % 2 != 0:print '%s: %s ---> %s' % (time.ctime(), self.new_name, num)time.sleep(1)else:self.queue.put(num)except:print '%s: %s get odd numbers finished!' % (time.ctime(), self.new_name) break class Even(threading.Thread):def __init__(self, queue):threading.Thread.__init__(self)self.queue = queueself.new_name = 'even_' + self.namedef run(self):while True:try:num = self.queue.get(1, 5)if num % 2 == 0:print '%s: %s ---> %s' % (time.ctime(), self.new_name, num)time.sleep(1)else:self.queue.put(num)except:print '%s: %s get even numbers finished!' % (time.ctime(), self.new_name) break if __name__ == '__main__':queue = Queue.Queue(10)t_producer = Producer(queue)t_odd = Odd(queue)t_even = Even(queue)t_producer.start()t_odd.start()t_even.start()

    ?

    轉載于:https://www.cnblogs.com/tobeone/p/8063854.html

    總結

    以上是生活随笔為你收集整理的Queue模块的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。