學習python的queue

來源:互聯網
上載者:User

  The Queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics. It depends on the availability of thread support in Python; see the threading module.(也就是說Queue是安全執行緒的)

  The module implements three types of queue, which differ only in the order in which the entries are retrieved. In a FIFO (普通隊列)queue, the first tasks added are the first retrieved. In a LIFO (棧) queue, the most recently added entry is the first retrieved (operating like a stack). With a PriorityQueue(優先順序隊列), the entries are kept sorted (using the heapq module) and the lowest valued entry is retrieved first.

  下面說說三個重要的函數:

  Queue.get([block[, timeout]])

  Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block(其它線程無法操作queue) 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.put(item[, block[, timeout]])

  Put item into the queue. If optional args block is true and timeout is None (the default), block(其它線程無法操作queue) if necessary until a free slot is available(一直到這個item在queue可用). If timeout is a positive number, it blocks at mosttimeout 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.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.(每次get()函數就會產生一個task,調用task_done()函數告訴隊列task 處理完畢)

  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()

  Blocks(這裡指的是當前線程) until all items in the queue have been gotten and processed(task_done()).

  The count of unfinished tasks goes up whenever an item is added to the queue. The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.