Python程序中的線程操做-線程隊列

Python程序中的線程操做-線程隊列

1、線程隊列

queue隊列:使用import queue,用法和進程Queue同樣python

當必須在多個線程之間安全地交換信息時,隊列在線程編程中尤爲有用。編程

2、先進先出

class queue.Queue(maxsize=0)安全

import queue

q=queue.Queue()
q.put('first')
q.put('second')
q.put('third')

print(q.get())
print(q.get())
print(q.get())
'''
結果(先進先出):
first
second
third
'''

3、後進先出

class queue.LifoQueue(maxsize=0)less

import queue

q=queue.LifoQueue()
q.put('first')
q.put('second')
q.put('third')

print(q.get())
print(q.get())
print(q.get())
'''
結果(後進先出):
third
second
first
'''

4、存儲數據時可設置優先級的隊列

class queue.PriorityQueue(maxsize=0)函數

4.1優先級隊列

import queue

q=queue.PriorityQueue()
#put進入一個元組,元組的第一個元素是優先級(一般是數字,也能夠是非數字之間的比較),數字越小優先級越高
q.put((20,'a'))
q.put((10,'b'))
q.put((30,'c'))

print(q.get())
print(q.get())
print(q.get())
'''
結果(數字越小優先級越高,優先級高的優先出隊):
(10, 'b')
(20, 'a')
(30, 'c')
'''

4.2更多方法說明

Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.fetch

The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0]). A typical pattern for entries is a tuple in the form: (priority_number, data).ui

exception queue.Empty: Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.this

exception queue.Full: Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full.spa

Queue.qsize()

Queue.empty(): return True if empty線程

Queue.full(): return True if full

Queue.put(item, block=True, timeout=None): Put item into the queue. If optional args block is true and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).

Queue.put_nowait(item): Equivalent to put(item, False).

Queue.get(block=True, timeout=None): Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).

Queue.get_nowait(): Equivalent to get(False).

Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.

Queue.task_done(): Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.

If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).

Raises a ValueError if called more times than there were items placed in the queue.

Queue.join(): block直到queue被消費完畢。

翻譯

優先級隊列的構造函數。 maxsize是一個整數,它設置能夠放置在隊列中的項數的上限。 一旦達到此大小,插入將阻塞,直到使用隊列項。 若是maxsize小於或等於零,則隊列大小爲無窮大。

首先檢索值最低的條目(值最低的條目是由已排序(列表(條目))[0]返回的條目)。 條目的典型模式是元組的形式:(priority_number, data)。

exception queue.Empty:在空隊列對象上調用非阻塞get()(或get_nowait())時引起異常。

exception queue.Full:當對已滿的隊列對象調用非阻塞put()(或put_nowait())時引起異常。

Queue.qsize()

Queue.empty():若是爲空返回True

Queue.full():若是已滿返回True

Queue.put(item, block=True, timeout=None):將項放入隊列。 若是可選的args塊爲true, timeout爲None(缺省值),則在空閒插槽可用以前,若是有必要,將阻塞。 若是timeout是一個正數,那麼它將阻塞最多的超時秒,若是在這段時間內沒有可用的空閒插槽,則引起完整的異常。 不然(block爲false),若是一個空閒插槽當即可用,則將一個項放到隊列中,不然引起徹底異常(在這種狀況下忽略超時)。

Queue.put_nowait(item):至關於put(item, False)。

Queue.get(block=True, timeout=None):從隊列中刪除並返回一個項。 若是可選的args塊爲true, timeout爲None(缺省值),則在項目可用以前,若是有必要,將阻塞。 若是timeout是一個正數,那麼它將阻塞最多的超時秒,若是在這段時間內沒有可用的項,則引起空異常。 不然(block爲false),返回一個當即可用的項,不然引起空異常(在這種狀況下忽略超時)。

Queue.get_nowait():至關於(False)。

提供了兩種方法來支持跟蹤已加入隊列的任務是否已被守護進程使用者線程徹底處理。

Queue.task_done():指示已完成先前排隊的任務。 由隊列使用者線程使用。 對於用於獲取任務的每一個get(),後續對task_done()的調用告訴隊列任務上的處理已經完成。

若是join()當前處於阻塞狀態,那麼在處理完全部項以後,它將繼續運行(這意味着對於已經放入隊列()的每一個項,都收到了task_done()調用)。

若是調用次數超過放置在隊列中的項的次數,則引起ValueError。

Queue.join(): block直到queue被消費完畢。

相關文章
相關標籤/搜索