SoFunction
Updated on 2024-12-19

Python3 queue queue module details

Introduction to queue

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

In python, the data between multiple threads is shared, multiple threads for data exchange, can not guarantee the security and consistency of the data, so when multiple threads need to exchange data, the queue appeared, the queue can be a perfect solution to the exchange of data between the threads, to ensure that the data between the threads of security and consistency.

Note: In this case, the module is called Queue.

The queue module has three types of queues and constructors

Python queue module for FIFO queues FIFO. (maxsize)

LIFO is similar to heap, i.e. first in, first out. (maxsize)

There is also a priority queue level where the lower the level the more it comes out first. (maxsize)

Common methods in the queue module

() Returns the size of the queue

() Returns True if the queue is empty, and False if it is not.

() Returns True if the queue is full, and False if it is not.

Corresponds to maxsize

([block[, timeout]]) Get the queue, take out an element immediately, timeout timeout

(item[, timeout]]) Write to queue, place an element immediately, timeout timeout

queue.get_nowait() is equivalent to (False)

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

() blocks the calling thread until all the tasks in the queue have been processed, which effectively means waiting until the queue is empty before doing anything else

queue.task_done() After completing a job, the queue.task_done() function sends a signal to the queue that the task has been completed

code example

The following code was passed under Python 3 with the

Creating a Queue

import queue
q = ()

empty method (returns True if the queue is empty)

import queue
q = ()
print(())
#exports:True

full method (returns True if the queue is full)

import queue
q = (1) # Specify the queue size
('a')
print(())
#exports:True

The put and get methods

import queue
q = ()
('a')
('b')
print(())
#exports:a

The qsize method (returns the number of elements in the queue).

import queue
q = ()
('a')
('b')
print(())
#exports:2

summarize

The above is a small introduction to the Python3 queue queue module in detail, I hope to help you, if you have any questions please leave me a message, I will reply to you in a timely manner. Here also thank you very much for your support of my website!