Python Queue
## Python3.x Python queue Module
In Python, the `queue` module provides a thread-safe queue implementation for safely passing data in multi-threaded programming.
A queue is a first-in-first-out (FIFO) data structure. The `queue` module provides multiple queue types, including `Queue`, `LifoQueue`, and `PriorityQueue`, to meet different needs.
* * *
## Queue Types
### 1. Queue
`Queue` is the most commonly used queue type in the `queue` module. It implements a standard first-in-first-out (FIFO) queue. Here is the basic usage of `Queue`:
## Example
import queue
# Create a queue
q = queue.Queue()
# Add elements to the queue
q.put(1)
q.put(2)
q.put(3)
# Get elements from the queue
print(q.get())# Output: 1
print(q.get())# Output: 2
print(q.get())# Output: 3
### 2. LifoQueue
`LifoQueue` is a last-in-first-out (LIFO) queue, similar to a stack. Here is the basic usage of `LifoQueue`:
## Example
import queue
# Create a LIFO queue
q = queue.LifoQueue()
# Add elements to the queue
q.put(1)
q.put(2)
q.put(3)
# Get elements from the queue
print(q.get())# Output: 3
print(q.get())# Output: 2
print(q.get())# Output: 1
### 3. PriorityQueue
`PriorityQueue` is a priority queue where elements are retrieved in priority order. Here is the basic usage of `PriorityQueue`:
## Example
import queue
# Create a priority queue
q = queue.PriorityQueue()
# Add elements to the queue, elements are tuples (priority, data)
q.put((3,'Low priority'))
q.put((1,'High priority'))
q.put((2,'Medium priority'))
# Get elements from the queue
print(q.get())# Output: (1, 'High priority')
print(q.get())# Output: (2, 'Medium priority')
print(q.get())# Output: (3, 'Low priority')
* * *
## Common Methods
### 1. `put(item, block=True, timeout=None)`
Put `item` into the queue. If `block` is `True` and the queue is full, wait for `timeout` seconds until there is space in the queue. If `timeout` is `None`, wait indefinitely.
### 2. `get(block=True, timeout=None)`
Get and remove an element from the queue. If `block` is `True` and the queue is empty, wait for `timeout` seconds until there is an element in the queue. If `timeout` is `None`, wait indefinitely.
### 3. `qsize()`
Return the number of elements in the queue.
### 4. `empty()`
Return `True` if the queue is empty, otherwise return `False`.
### 5. `full()`
Return `True` if the queue is full, otherwise return `False`.
* * *
## Thread Safety
All queue types in the `queue` module are thread-safe, which means multiple threads can safely operate on the same queue at the same time without additional synchronization mechanisms. This makes the `queue` module an ideal choice for passing data in multi-threaded programming.
* * *
## Example: Multi-threaded Queue
The following is an example of using `Queue` to pass data between multiple threads:
## Example
import queue
import threading
import time
# Create a queue
q = queue.Queue()
# Producer thread
def producer():
for i in range(5):
print(f'Produced {i}')
q.put(i)
time.sleep(1)
# Consumer thread
def consumer():
while True:
item = q.get()
if item is None:
break
print(f'Consumed {item}')
q.task_done()
# Start producer thread
producer_thread =threading.Thread(target=producer)
producer_thread.start()
# Start consumer thread
consumer_thread =threading.Thread(target=consumer)
consumer_thread.start()
# Wait for producer thread to finish
producer_thread.join()
# Wait for all tasks in the queue to complete
q.join()
# Send end signal
q.put(None)
consumer_thread.join()
* * *
## Common Attributes and Methods
The following is a table description of common classes, methods, and attributes of the Python queue module (thread-safe queue), including function descriptions and examples:
### Core Classes of queue Module
| Class | Description | Use Case |
| --- | --- | --- |
| **`queue.Queue`** | First-in-first-out (FIFO) queue | General task queue |
| **`queue.LifoQueue`** | Last-in-first-out (LIFO) queue (similar to stack) | Scenarios requiring LIFO |
| **`queue.PriorityQueue`** | Priority queue (implemented with min-heap) | Process tasks by priority |
| **`queue.SimpleQueue`** | Simpler FIFO queue (Python 3.7+) | Scenarios without advanced features |
### Common Methods (Supported by All Queue Classes)
| Method | Description | Example | Return Value |
| --- | --- | --- | --- |
| **`put(item)`** | Put element into queue | `q.put("task1")` | None |
| **`get()`** | Get and remove element | `item = q.get()` | Queue element |
| **`empty()`** | Check if queue is empty | `if q.empty():` | `True`/`False` |
| **`full()`** | Check if queue is full | `if q.full():` | `True`/`False` |
| **`qsize()`** | Return current queue size | `size = q.qsize()` | Integer |
| **`task_done()`** | Mark task as done (used with `join()`) | `q.task_done()` | None |
| **`join()`** | Block until all tasks are done | `q.join()` | None |
### Blocking Control Parameters
| Parameter | Description | Default Value | Example |
| --- | --- | --- | --- |
| `block` | Whether to block when queue is empty/full | `True` | `q.get(block=False)` |
| `timeout` | Blocking timeout in seconds | `None` | `q.put(x, timeout=5)` |
### PriorityQueue Specific Usage
**Element format:** (priority, data), smaller priority value comes out first
## Example
pq = queue.PriorityQueue()
pq.put((1,"low"))
pq.put((0,"high"))
print(pq.get())# Output: "high"
### Example
Producer-Consumer model:
## Example
import queue,threading
q = queue.Queue(maxsize=3)# Queue with capacity of 3
def producer():
for i in range(5):
q.put(f"Task-{i}")
print(f"Produced: Task-{i}")
def consumer():
while True:
item = q.get()
print(f"Consumed: {item}")
q.task_done()
threading.Thread(target=producer, daemon=True).start()
threading.Thread(target=consumer, daemon=True).start()
q.join()# Wait for all tasks to complete
Priority task processing:
## Example
pq = queue.PriorityQueue()
pq.put((3,"Scan"))
pq.put((1,"Emergency"))
pq.put((2,"Log"))
while not pq.empty():
print(pq.get())# Output order: Emergency β Log β Scan
Non-blocking get (to avoid deadlock):
## Example
try:
item = q.get_nowait()# Equivalent to q.get(block=False)
except queue.Empty:
print("Queue is empty")
YouTip