1. 程式人生 > 實用技巧 >Python程式中的執行緒操作-執行緒佇列

Python程式中的執行緒操作-執行緒佇列

先進先出 code
import queue
 
q=queue.Queue()
q.put('first')
q.put('second')
q.put('third')
 
print(q.get())
print(q.get())
print(q.get())
outputs
macname@MacdeMacBook-Pro py % python3 cccccc.py
first
second
third
macname@MacdeMacBook-Pro py %

先進後出 code
import queue
 
q=queue.LifoQueue()
q.put(
'first') q.put('second') q.put('third') print(q.get()) print(q.get()) print(q.get())
outputs
macname@MacdeMacBook-Pro py % python3 cccccc.py
third
second
first
macname@MacdeMacBook-Pro py %
可設定優先順序的佇列 code
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())
outputs
macname@MacdeMacBook-Pro py % python3 cccccc.py
(10, 'b')
(20, 'a')
(30, 'c')
macname@MacdeMacBook-Pro py %

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. The lowest valued entries are retrieved first (the lowest valued entry is the one returned bysorted(list(entries))[0]). A typical pattern for entries is a tuple in the form: (priority_number, data). exception queue.Empty: Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty. exception queue.Full: Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full. 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被消費完畢。