Python Queue Module

Source: Internet
Author: User

Create a "queue" object

Import Queue
Myqueue = queue.queue (maxsize = 10)

The Queue.queue class is a synchronous implementation of a queue. The queue length can be unlimited or limited. The queue length can be set through the optional parameter maxsize of the queue's constructor. If MaxSize is less than 1, it means that the queue length is infinite.

Put a value in the queue

Myqueue.put (10)

The put () method of the call queue object inserts an item at the end of the team. Put () has two parameters, the first item is required, the value of the inserted item, the second block is an optional parameter, and the default is 1. If the queue is currently empty and the Block is the 1,put () method, the calling thread pauses until a data cell is vacated. If the block is the 0,put method, the full exception is thrown.

Take a value out of the queue

Myqueue.get ()

The Get () method of the call queue object is removed from the team header and returns an item. The optional parameter is block, which is true by default. If the queue is empty and the Block is True,get (), the calling thread is paused until a project is available. If the queue is empty and the block is false, the queue throws an empty exception.


There are three types of queues in the Python queue module:
1, the Python queue module FIFO queuing first-out.
2. LIFO is similar to heap. That is, advanced back out.
3, there is a priority queue level lower the more first out.

There are three constructors for each of the three queues:
1, Class Queue.queue (maxsize) FIFO
2, Class Queue.lifoqueue (maxsize) LIFO
3, Class Queue.priorityqueue (maxsize) Priority queue

Describe the common methods in this package:

Queue.qsize () returns the size of the queue
Queue.empty () returns True if the queue is empty, and vice versa false
Queue.full () returns True if the queue is full, otherwise false
Queue.full corresponds to maxsize size
Queue.get ([block[, timeout]]) Get queue, timeout wait time
Queue.get_nowait () quite queue.get (False)
Non-blocking Queue.put (item) write queue, timeout wait time
Queue.put_nowait (item) quite Queue.put (item, False)
Queue.task_done () After completing a work, the Queue.task_done () function sends a signal to the queue that the task has completed
Queue.join () actually means waiting until the queue is empty before performing another operation

Python Queue Module

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.