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 in the queue are retrieved.

In the FIFO queue. Retrieve entries in FIFO 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 lowest value entries are 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. Represents the maximum number of entries for a queue. Once the queue is full, the insert will be blocked until the spare space exists in the queue. Assume that 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 the spare space exists in the queue. Assuming maxsize is less than or equal to 0, the queue size is infinite.


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 the spare space exists in the queue.

Assuming maxsize is less than or equal to 0, the queue size is infinite.


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-clogging get () or get_nowait () is called when the queue is empty.


Exception queue. Full
When the queue is full, the non-clogging 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. Same. Qsize () < maxsize also cannot guarantee that the put () will not be blocked.

Queue.empty ()
Assume that the queue is empty. Returns true, otherwise false. Assuming that empty () returns True, there is no guarantee that the next put () call will not be blocked. Similar to. 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. Assuming that full () returns True, there is no guarantee that the next get () call will not be blocked.

Similarly, full () returns false also 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. Assume that block is true and timeout is none. This method will wait until there is a queue with free space. Assuming that timeout is a positive integer, the method plugs up to timeout seconds and throws a full exception. Assuming that 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. Assuming that block is true and timeout is None, the method will block until an entry in the queue is available. Assuming that timeout is a positive integer, the method will clog up to timeout seconds and throw an empty exception. Suppose the block is false and the queue is empty. The empty exception is thrown directly (then timeout is ignored).



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

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


Queue.task_done ()
Indicates that a task in a previous queue is complete. 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 is complete.


Assuming that the join () call is blocking, when all entries in the queue are processed it will resume running (meaning that the task_done () call will be received by each entry in the queue).


The ValueError exception is thrown if the number of calls is exceeded in the queue.

Queue.join ()
Block until all entries in the queue are fetched and processed.
When an entry is added to the queue, the count of the unfinished task is added. When a consumer thread calls Task_done (), the Count of the unfinished task is lowered.

When the count of unfinished tasks drops to 0 o'clock, join () unlocks.

The following is a detailed example. For instructions on how to wait for the 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 ()       # blocked until all tasks are complete

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.