The queue module in Python realizes multi-producer and multi-consumer models, which is very practical when multi-thread programming is needed. Moreover, the queue class in this module implements the lock primitive, so there is no need to consider the security of multithreading.
There are three types of queues built into the module, namely class queues. Queue (maxsize=0), class queue. Liofqueue (maxsize = 0) and classqueue. Priority queue (maxsize = 0). The difference between the three is that the order of taking them out is inconsistent.
Queue is a FIFO queue, and tasks are taken out in the order of addition.
LifoQueue is a LIFO queue, which is similar to a stack. The added tasks are taken out first.
PriorityQueue is a priority queue. Tasks in the queue are sorted by priority, and the tasks with high priority are taken out first.
As you can see, there are three different types of built-in queues mentioned above, in which maxsize is an integer to set the upper limit of the number of tasks that can be put into the queue. When this size is reached, the insert operation will prevent the tasks in the queue from being used. If maxsize is less than or equal to zero, the queue size is infinite.
To add a task to the queue, just call the put () function directly.
The complete function signature of the put () function is as follows: queue.put (item, block = true, timeout = none). As you can see, this function has two optional parameters.
By default, when the queue is full, this function will block until there is room in the queue to add tasks. If the timeout is positive, it will prevent the timeout for up to two seconds, and if there is no vacant position within this time, a complete exception will be thrown.
When block is false, the timeout parameter will be invalid. At the same time, if there is no extra place in the queue to add a task, a full exception will be thrown, otherwise the task will be put directly in the queue without blocking the return.
In addition, you can add tasks through Queue.put_nowait(item), which is equivalent to Queue.put(item, False), so I won't go into details here. Similarly, when the queue is full, this operation will throw a full exception.
To get the task from the queue, just call the get () function directly.
Like the put () function, the get () function has two optional parameters, and the complete signature is as follows: queue.get (block = true, timeout = none).
By default, calling this function when the queue is empty will block until a task is available in the queue. If timeout is a positive number, it will block the timeout for up to seconds, and if there are no tasks available within this time, an empty exception will be thrown.
When block is false, the timeout parameter will be invalid. At the same time, if there is no task available in the queue, an empty exception will be thrown immediately, otherwise a task will be obtained directly without blocking the return.
In addition, you can also get tasks through Queue.get_nowait (), which is equivalent to Queue.get(False), so I won't go into details here. Similarly, when the queue is empty, the operation will throw an empty exception.
The function returns the size of the queue. Note that this size is not accurate, and qsize() > 0 does not guarantee that the subsequent get () will not be blocked. Similarly, qsize ()
Returns True if the queue is empty, otherwise returns False. If empty () returns True, there is no guarantee that the put () of subsequent calls will not be blocked. Similarly, if empty () returns False, there is no guarantee that the subsequent call of get () will not be blocked.
Returns True if the queue is full, otherwise returns False. If full () returns True, there is no guarantee that the get () of subsequent calls will not be blocked. Similarly, if full () returns False, there is no guarantee that the put () of subsequent calls will not be blocked.
Line up. Queue () is a FIFO queue, and the dequeue order is the same as the enqueue order.
Line up. Liofqueue () is Liofqueue, and the dequeue order is completely opposite to the enqueue order, similar to the stack.
The order of tasks in the priority queue has nothing to do with the order when they are put in, but is sorted according to the size of tasks, and the minimum value is taken out first. What are the rules of task size?
Note that because the comparison rules of lists are compared in subscript order, before comparing the sizes, the element types of all lists in the queue corresponding to subscript positions should be the same.
For example, [2, 1] and [" 1 ","b"] cannot be placed in the priority queue because the element types in the first position are different.
However, for [2, 1] and [1, "b"], even if the type of the second element is inconsistent, it can be put into the priority queue, because only the size of the first position element can be compared, and the size of the second position element is not needed.
However, [2, 1] and 1 [2, "b"] are also not allowed to be put into the priority queue, because the elements in the second position need to be compared to compare the results. However, the element types in the second position are inconsistent, and the sizes cannot be compared.
To sum up, that is to say, before comparing the results, the element types of the corresponding subscript positions need to be consistent.
We define an animal type, hoping to sort it by age. The younger the age, the higher the priority.
This chapter introduces queues and their common operations. Because the queue implements the lock primitive by default, there is no need to consider the security of multithreading in multithreading programming, which is quite friendly to programmers.