Python multithreaded 3:queue

Source: Internet
Author: User

The queue module implements multi-producer, multi-consumer queues. In multi-threaded environment, this queue can realize the exchange of information between multiple threads safely.

Queue Module Introduction
The module implements 3 types of queues, which differ in the order in which entries are retrieved in the queue. In FIFO queues, the entries are retrieved in first in, out order. In the LIFO queue, the last entry added is first retrieved (the operation is similar to a stack). In the priority queue, entries are saved as ordered (using the HEAPQ module) and the minimum entry is retrieved first.
The queue module defines the following classes and exceptions:
Class queue. Queue (maxsize=0)
The constructor for the FIFO queue. MaxSize is an integer that represents the maximum number of entries in the queue. Once the queue is full, the insert will be blocked until there is free space in the queue. If MaxSize is less than or equal to 0, the queue size is unlimited.
Class queue. Lifoqueue (maxsize=0)
The constructor for the LIFO queue. MaxSize is an integer that represents the maximum number of entries in the queue. Once the queue is full, the insert will be blocked until there is free space in the queue. If MaxSize is less than or equal to 0, the queue size is unlimited.
Class queue. Priorityqueue (maxsize=0)
The constructor for the priority queue. MaxSize is an integer that represents the maximum number of entries in the queue. Once the queue is full, the insert will be blocked until there is free space in the queue. If MaxSize is less than or equal to 0, the queue size is unlimited.
The entry of the minimum value is first retrieved (the entry of the minimum value is the entry returned by sorted (list (entries)) [0]. Usually an entry is saved in the following form: (Priority_number, data).
Exception queue. Empty
The exception is thrown when a non-blocking get () or get_nowait () is called when the queue is empty.
Exception queue. Full
When the queue is full, the non-blocking put () or put_nowait () is called, and the exception is thrown.
Queue Object
The Queue object (queue, Lifoqueue, or Priorityqueue) provides the following methods:


queue.qsize ()
Returns the approximate size of the queue. Note that qsize () > 0 does not guarantee that the next get () method will not be blocked, nor does the Qsize () < maxsize guarantee that the put () is not blocked.

Queue.empty ()
Returns true if the queue is empty, otherwise false. If empty () returns True, there is no guarantee that the next put () call will not be blocked. Similarly, empty () returns false and does not guarantee that the next get () call will not be blocked.

queue.full ()
Returns true if the queue is full, otherwise false. If full () returns True, there is no guarantee that the next get () call will not be blocked. Similarly, full () returns false and does not guarantee that the next put () call will not be blocked.

queue.put (item, Block=true, Timeout=none)
Put the item into the queue. If block is true and timeout is None, the method waits until there is a queue with free space. If timeout is a positive integer, the method blocks up to timeout seconds and throws a full exception. If the block is false and the queue is full, it throws an outright exception (timeout is ignored).

queue.put_nowait (item)
Equivalent to put (item, False).

Queue.get (block=true, Timeout=none)
The removal from the queue is returned with an entry. If block is true and timeout is None, the method will block until there are entries available in the queue. If timeout is a positive integer, the method will block a maximum of timeout seconds and throw an empty exception. If the block is false and the queue is empty, the empty exception is thrown directly (at which time timeout is ignored).

queue.get_nowait ()
Equivalent to get (False).

If you need to track whether a task entered into the queue has been processed by the sprite consumer thread, you can use the two methods provided below:


Queue.task_done ()
Represents the completion of a task in a previous queue. Used by queue consumer threads. For each get () get to the task, the next call to Task_done () tells the queue that the processing of the task has been completed.
If a join () call is blocking, when all entries in the queue are processed it resumes execution (meaning that the task_done () call is received by each entry in the queue).
A ValueError exception is thrown if the number of calls exceeds the amount of entries placed in the queue.

Queue.join ()
Blocks until all entries in the queue are fetched and processed.
When an entry is added to the queue, the count of unfinished tasks increases. When a consumer thread calls Task_done (), the count of unfinished tasks is reduced. When the count of unfinished tasks is reduced to 0 o'clock, join () unlocks.

Here is a concrete example of how to wait for a queue task to complete:
def worker (): While    True:        item = q.get ()        do_work (item)        q.task_done () q = Queue () for I in range (num_ Worker_threads):     t = Thread (target=worker)     T.daemon = True     t.start () for item in Source ():    q.put (item) Q.join ()       # blocks until all tasks are completed

Python multithreaded 3:queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.