42. Python Queue 模塊
阿新 • • 發佈:2017-12-19
queuePython提供了Queue模塊來專門實現消息隊列Queue對象
Queue對象實現一個fifo隊列(其他的還有lifo、priority隊列,這裏不再介紹)。
queue只有maxsize一個構造參數,用來指定隊列容量,指定為0的時候代表容量無限。
主要有以下成員函數:
Queue.qsize():返回消息隊列的當前空間。返回的值不一定可靠。
Queue.empty():判斷消息隊列是否為空,返回True或False。同樣不可靠。
Queue.not_empty():判斷消息隊列是否為非空。同上不可靠。
Queue.full():類似上邊,判斷消息隊列是否滿。
Queue.put(item, block=True, timeout=None):往消息隊列中存放消息。block可以控制是否阻塞,timeout指定阻塞時候的等待時間。如果不阻塞或者超時,會引起一個full exception。
Queue.put_nowait(item):相當於put(item, False).
Queue.get(block=True, timeout=None):獲取一個消息,其他同put。
Queue.task_done():接收消息的線程通過調用這個函數來說明消息對應的任務已完成。
Queue.join():表示等待,等到隊列為空,在執行別的操作。
Queue.terminate():表示強制關閉。
例子:
#!/usr/bin/env python # -*- coding:utf-8 -*- from multiprocessing import Queue from threading import Thread import time """ 寫一個消費者和生產者, 用多線程方式實現, 通過類的重寫的方法實現。 """ class Proceducer(Thread): def __init__(self, queue): super(Proceducer,self).__init__() self.queue = queue def run(self): try: for i in xrange(1, 10): print ("put data is {0} to queue".format(i)) self.queue.put(i) except Exception as e: print ("put data error") raise e class Consumer_even(Thread): def __init__(self, queue): super(Consumer_even , self).__init__() self.queue = queue def run(self): try: while not self.queue.empty(): number = self.queue.get(block=True, timeout=3) if number % 2 != 0: print("get {0} from queue EVEN, thread name is {1}".format(number, self.getName())) else: self.queue.put(number) time.sleep(1) except Exception as e: raise e class Consumer_odd(Thread): def __init__(self, queue): super(Consumer_odd , self).__init__() self.queue = queue def run(self): try: while not self.queue.empty(): number = self.queue.get(block=True, timeout=3) if number % 2 == 0: print("get {0} from queue ODD".format(number)) else: self.queue.put(number) time.sleep(1) except Exception as e: raise e def main(): queue = Queue() p = Proceducer(queue=queue) p.start() p.join() time.sleep(1) c1 = Consumer_even(queue=queue) c2 = Consumer_odd(queue=queue) c1.start() c2.start() c1.join() c2.join() print ("All thread terminate!") if __name__ == '__main__': main()
結果:
put data is 1 to queue put data is 2 to queue put data is 3 to queue put data is 4 to queue put data is 5 to queue put data is 6 to queue put data is 7 to queue put data is 8 to queue put data is 9 to queue get 1 from queue EVEN, thread name is Thread-2 get 2 from queue ODD get 3 from queue EVEN, thread name is Thread-2 get 4 from queue ODD get 5 from queue EVEN, thread name is Thread-2 get 6 from queue ODD get 7 from queue EVEN, thread name is Thread-2 get 8 from queue ODD get 9 from queue EVEN, thread name is Thread-2 All thread terminate!
42. Python Queue 模塊