This article has participated in the weekend study program, click the link to see details: Weekend study

The official documentation docs.python.org/3.6/library…

A queue is a derived list that has a first-in, first-out (FIFO) data structure compared to a regular list, i.e. the earliest element to enter the queue is the earliest to be extracted and processed. Queues can only insert elements from the end of the queue and delete elements from the beginning of the queue. It’s like waiting in line at a bank, where the first person to be handed in is the first to do the business, and the others line up by number until it’s their turn.

Python3 uses the queue module to implement the function of queues. Queue.Queue is usually used.

Q1 = queue.Queue(maxsize = 10) Specify the queue length by maxsize. Q2 = queue.queue () q2 = queue.queue () Maxsize is not specified, and the queue length is infinite.Copy the code

Puts a value into the queue at the end of the queue.

Q.put (10) # The value 10 can also be put into other types of values, such as strings, lists, tuples, etc.Copy the code

Removes a value from the queue at the head of the queue

Q.get () ##Copy the code

It is worth mentioning that queues are commonly used for multithreaded communication due to their nature. The Queue is thread-safe because it deletes the data internally each time it uses get() to make sure it doesn’t get duplicate data the next time. Although lists or tuples can be used as storage structures for threads, they are not thread safe and need to be locked to ensure thread safety.

Q.q size() # return queue size q.null () # Return True if the queue is full, False otherwise. Note that if the queue contains elements but does not reach maxsize, False will still be returned. Q.email () # Return True if the queue is empty, False otherwise q.task_done() # The function sends a signal to the queue after it has finished its work q.jin () # Wait until the queue is empty before performing any other operations.Copy the code

There is another common attribute that you can use if you want to know the specific element in the queue: q.kueue