Python3 queue module details, python3queue

Source: Internet
Author: User

Python3 queue module details, python3queue

Queue Introduction

Queue is a standard library in python, commonly known as queue.

In python, data between multiple threads is shared. When multiple threads exchange data, data security and consistency cannot be guaranteed, therefore, when multiple threads need to exchange data, the queue will appear. The queue can perfectly solve the data exchange between threads and ensure the security and consistency of data between threads.

Note: In python2.x, the module name is Queue.

The queue module has three queues and constructor functions.

The first-in-first-out queue of the Python queue module. Queue. Queue (maxsize)

LIFO is similar to a heap, that is, first and then output. Queue. LifoQueue (maxsize)

The lower the priority queue level, the more advanced it is. Queue. PriorityQueue (maxsize)

Common methods in the queue Module

Queue. qsize () returns the size of the queue

Queue. empty () if the queue is empty, True is returned. Otherwise, False is returned.

Queue. full () if the queue is full, True is returned. Otherwise, False is returned.

Queue. full corresponds to maxsize

Queue. get ([block [, timeout]) gets the queue and retrieves an element immediately.

Queue. put (item [, timeout]) is written to the queue, and an element is put immediately.

Queue. get_nowait () is equivalent to queue. get (False)

Queue. put_nowait (item) is equivalent to queue. put (item, False)

Queue. join () blocks the calling thread until all tasks in the queue are processed. In fact, it means that other operations are performed when the queue is empty.

After queue. task_done () completes a job, the queue. task_done () function sends a signal to the completed queue of the job.

Code instance

Run the following code in Python3:

Create a queue

import queueq = queue.Queue()

Empty method (True if the queue is empty)

Import queueq = queue. Queue () print (q. empty () # output: True

Full method (returns True if the queue is full)

Import queueq = queue. Queue (1) # specify the queue size q. put ('A') print (q. full () # output: True

Put and get Methods

Import queueq = queue. Queue () q. put ('A') q. put ('B') print (q. get () # output:

Qsize method (returns the number of elements in the queue)

Import queueq = queue. Queue () q. put ('A') q. put ('B') print (q. qsize () # output: 2

Summary

The above is a detailed introduction of the Python3 queue module introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.