A queue is a linear table that only allows insert operations at one end and delete operations at the other.
A search for queue in the Python documentation will reveal that the Python standard library contains four queues, / / / .
deque is the abbreviation of double-ended queue (double-ended queue), because both ends can be edited, deque can be used to implement both stack (stack) and queue (queue).
deque supports rich operation methods, the main methods are shown in the figure:
The deque implementation has lower time and space complexity than the list implementation of a queue. list implementation has a space complexity of about O(n) for out (pop) and insert, and deque has a time complexity of O(1) for out (pop) and in (append).
The deque also supports the in operator, which can be written as follows:
q = ([1, 2, 3, 4]) print(5 in q) # False print(1 in q) # True
deque also encapsulates a method for rotating clockwise and counterclockwise: rotate.
# Clockwise q = ([1, 2, 3, 4]) (1) print(q) # [4, 1, 2, 3] (1) print(q) # [3, 4, 1, 2] # Counterclockwise q = ([1, 2, 3, 4]) (-1) print(q) # [2, 3, 4, 1] (-1) print(q) # [3, 4, 1, 2]
In terms of thread safety, methods such as append() and pop() in are atomic operations, so they are GIL-protected thread-safe methods.
static PyObject * deque_append(dequeobject *deque, PyObject *item) { Py_INCREF(item); if (deque_append_internal(deque, item, deque->maxlen) < 0) return NULL; Py_RETURN_NONE; }
As you can see with the dis method, append is an atomic operation (one line of bytecode).
In summary, is a data structure that can be easily implemented as a queue, is thread-safe, and has high performance.
&
and are both queues that support multiple producers and consumers, based on the fact that they both provide Queue (FIFO queue), PriorityQueue (PriorityQueue), LifoQueue (LIFO queue), and the same in terms of interface.
The difference is that it applies to multi-threaded scenarios, and it applies to communication in concurrent scenarios. Due to the addition of asyncio, the blocking interface in the middle of the process is executed in a way that returns the concurrent object, and the specific differences are listed in the following table:
Multiprocessing provides three queues, Queue, SimpleQueue, and JoinableQueue.
Both thread-safe and process-safe, equivalent to a multiprocess clone of . And very similar, supports put and get operations, the underlying structure is.
The bottom layer is built based on Pipe, but the data transfer is not directly written to the Pipe, but written to the process of the local buffer, through a feeder thread written to the bottom layer Pipe, this is to realize the timeout control and non-blocking put/get, so the Queue provides join_thread, cancel_join_thread, close function to control the behavior of the feeder, close function is used to close the feeder thread, join_thread is used to join the feeder thread, cancel_join_thread, close function is used to control the behavior of the feeder. thread, cancel_join_thread, and close functions to control the behavior of the feeder. close function is used to close the feeder thread, join_thread is used to join the feeder thread, and cancel_join_thread is used to not automatically join the feeder thread when the process exits from the control, and using cancel_join_thread has the effect of controlling the timeout and non-blocking put/get. join_thread may result in data loss due to some data not being written to the Pipe by the feeder.
Unlike, join() and task_done operations are not supported by default, which require the use of objects.
SimpleQueue is a simplified queue , remove the buffer in the Queue , no use of Queue may have problems , but put and get methods are blocking and no timeout control .
summarize
By comparison, we can find that the above four structures all implement queues, but the usefulness of each has its own bias, in the data structure level of the queue, but there is no application scenarios to support the queue can be seen as a basic data structure. queue module implements the queue for multi-production threads, multi-consumption threads, the module implements the queue for multiple production and consumption of concurrent threads, and the module implements the queue for multi-production concurrent processes, multi-consumption concurrent processes and the module implements the queue for multi-production processes, multi-consumption processes. queue module implements queues for multi-production and multi-consumer processes, while the module implements queues for multi-production and multi-consumer processes.
The above is a small introduction to the four types of queues in Python, I hope to help you, if you have any questions please leave me a message, I will reply to you in a timely manner. I would also like to thank you very much for your support of my website!