python模塊學習(queue模塊的Q

學習版本3.5.2

PriorityQueue類和LifoQueue類繼承Queue類然後重寫了_init、_qsize、_put、_get這四個類的私有方法

Queue:先進先出隊列的同步實現,通過雙向列表實現的

    # Initialize the queue representation      def _init(self, maxsize):          self.queue = deque()        def _qsize(self):          return len(self.queue)        # Put a new item in the queue      def _put(self, item):          self.queue.append(item)        # Get an item from the queue      def _get(self):          return self.queue.popleft()

PriorityQueue:優先隊列的同步實現,通過堆實現

    def _init(self, maxsize):          self.queue = []        def _qsize(self):          return len(self.queue)        def _put(self, item):          heappush(self.queue, item)        def _get(self):          return heappop(self.queue)

LifoQueue:後進先出隊列的同步實現,通過list來實現的

    def _init(self, maxsize):          self.queue = []        def _qsize(self):          return len(self.queue)        def _put(self, item):          self.queue.append(item)        def _get(self):          return self.queue.pop()

__init__(self, maxsize=0) 初始化隊列長度,maxsize為0的時候長度為無限

empty(self) 返回隊列是否為空

full(self) 返回隊列是否為滿

qsize(self) 返回隊列的大小(並不可靠)

get(self, block=True, timeout=None) 從隊頭獲取並刪除元素,block為true:timeout為None時候,阻塞當前線程直到隊列中有可用元素;timeout為非負時候,等了timeout的時間還沒有可用元素時候拋出一個Empty異常;block為false:timeout為None時候,隊列為空則拋出Empty異常;timeout為非負時候,等待timeout時候後沒有可用元素則拋出Empty異常。

get_nowait(self) #return self.get(block=False)

put(self, item, block=True, timeout=None) 在隊尾插入一個元素,block為true:timeout為None時候,阻塞當前線程直到隊列中有可用位置;timeout為非負時候,等了timeout時間還沒有可用位置時候拋出一個Full異常;block為false:timeout為None時候,隊列沒有位置則拋出Full異常;timeout為非負時候,等待timeout時候後還是沒有可用位置則拋出Full異常。

put_nowait(self, item) #return self.put(item, block=False)

join(self) 阻塞當前線程直到隊列的任務全部完成了

task_done(self) 通知隊列任務的完成情況,當完成時候喚醒被join阻塞的線程

實驗代碼

#/usr/bin/python3  # -*- utf-8 -*-  #學習版本3.5.2    import threading  import time  import queue    que = queue.Queue(5)  #que = queue.PriorityQueue(5)  #que = queue.LifoQueue(5)    def que_put(item):      que.put(item)      print("put", item)      time.sleep(1)    def que_get():      item = que.get()      print("get", item)      que.task_done()    def que_join():      que.join()      print("task was done")    if __name__ == "__main__":      putthreadlist = []      getthreadlist = []      tlist = [4,5,6,1,2,3]      for i in tlist:          putthreadlist.append(threading.Thread(target=que_put,args=(i,)))      for i in range(6):          getthreadlist.append(threading.Thread(target=que_get))          for tl in putthreadlist:          tl.start()        threading.Thread(target=que_join).start()        for tl in getthreadlist:          tl.start()        print("main")

運行結果

put 4  put 5  put 6  put 1  put 2  get 4  put 3  get 5  get 6  get 1  get 2  get 3  main  task was done

que = queue.PriorityQueue(5)時候的運行結果

put 4  put 5  put 6  put 1  put 2  get 1  put 3  get 2  get 3  get 4  get 5  get 6  main  task was done

que = queue.LifoQueue(5)時候的運行結果

put 4  put 5  put 6  put 1  put 2  get 2  put 3  get 3  get 1  get 6  get 5  get 4  main  task was done