Python3 queue queue module details
- 2020-06-23 01:01:13
- OfStack
queue introduction
queue is the standard library in python, commonly known as queues.
In python, data are Shared between multiple threads, multiple threads for data exchange, will not be able to ensure the consistency and security of data, so when multiple threads need to exchange data, queue, for queue can perfectly solve the data exchange between threads, the consistency ensure the security of data between threads and.
Note: In python2.x, the module is named Queue
The queue module has three queues and constructors
First in, first out (FIFO) queue for Python queue module. queue. Queue (maxsize)
LIFO is similar to heap, i.e., in and out. queue. LifoQueue (maxsize)
The other is that the lower the priority queue, the lower the priority queue. queue. PriorityQueue (maxsize)
Common methods in the queue module
queue. qsize() returns the size of the queue
queue.empty () returns True if the queue is empty and False if not
queue.full() returns True if the queue is full, and False if not
queue.full corresponds to the size of maxsize
queue.get([block[, timeout]]) gets the queue, immediately pulls out 1 element, timeout timeout time
queue.put(item[, timeout]]) writes to the queue, immediately putting 1 element, timeout timeout time
queue.get_nowait() equivalent to ES79en. get(False)
queue.put_nowait(item) equivalent to ES88en.put (item, False)
queue.join () blocks the calling thread until all the tasks in the queue are processed, which actually means waiting until the queue is empty before doing anything else
queue.task_done () after completing a task, the queue.task_done () function sends a signal to the queue where the task has completed
The code examples
The following code is passed under Python3
Create a queue
import queue
q = queue.Queue()
empty method (returns True if the queue is empty)
import queue
q = queue.Queue()
print(q.empty())
# Output: True
full method (returns True if the queue is full)
import queue
q = queue.Queue(1) # Specify the queue size
q.put('a')
print(q.full())
# Output: True
put method and get method
import queue
q = queue.Queue()
q.put('a')
q.put('b')
print(q.get())
# Output: a
qsize method (returns the number of elements in the queue)
import queue
q = queue.Queue()
q.put('a')
q.put('b')
print(q.qsize())
# Output: 2
conclusion