【python】】Python 的 queue 模块使用笔记

简介: 【python】】Python 的 queue 模块使用笔记

@[python](Python 的 queue 模块使用笔记)

Python 的 queue 模块提供了同步队列实现,用于在多线程编程中安全地交换数据。以下是 queue 模块的一些主要类型和用法。


一、queue 模块

queue 模块包含以下几种类型的队列:

  • queue.Queue:先进先出(FIFO)队列。
  • queue.LifoQueue:后进先出(LIFO)队列,类似于栈。
  • queue.PriorityQueue:优先级队列,按优先级顺序处理元素。

二、示例代码

2.1. queue.Queue

这是一个典型的 FIFO 队列示例:

import time
import threading
import queue

# 创建一个 FIFO 队列
qFifo = queue.Queue(maxsize=10)

def producer():
    for i in range(10):
        qFifo.put(i)
        print(f'Produced {i}')
        time.sleep(1)

def consumer():
    while True:
        item = qFifo.get()
        if item is None:
            break
        print(f'Consumed {item}')
        qFifo.task_done()

# 创建生产者和消费者线程
prod_thread = threading.Thread(target=producer)
cons_thread = threading.Thread(target=consumer)

prod_thread.start()
cons_thread.start()

prod_thread.join()

# 停止消费者线程
qFifo.put(None)
cons_thread.join()

2.2. queue.LifoQueue

这是一个后进先出(LIFO)队列示例:

import queue
import threading
import time

# 后进先出(LIFO)队列
qLifo = queue.LifoQueue(maxsize=10)

def producer():
    for i in range(10):
        qLifo.put(i)
        print(f'Produced {i}')
        time.sleep(1)

def consumer():
    while True:
        item = qLifo.get()
        if item is None:
            break
        print(f'Consumed {item}')
        qLifo.task_done()

# 创建生产者和消费者线程
prod_thread = threading.Thread(target=producer)
cons_thread = threading.Thread(target=consumer)

prod_thread.start()
cons_thread.start()

prod_thread.join()

# 停止消费者线程
qLifo.put(None)
cons_thread.join()

2.3. queue.PriorityQueue

这是一个优先级队列示例:

import queue
import threading
import time

# 优先级队列,按优先级顺序处理元素
qPrio = queue.PriorityQueue()

def producer():
    for i in range(10):
        qPrio.put((i, f'Item {i}'))
        print(f'Produced Item {i}')
        time.sleep(1)

def consumer():
    while True:
        item = qPrio.get()
        if item is None:
            break
        priority, value = item
        print(f'Consumed {value}')
        qPrio.task_done()

# 创建生产者和消费者线程
prod_thread = threading.Thread(target=producer)
cons_thread = threading.Thread(target=consumer)

prod_thread.start()
cons_thread.start()

prod_thread.join()

# 停止消费者线程
qPrio.put(None)
cons_thread.join()

三、主要方法

  • put(item, block=True, timeout=None):将 item 放入队列。block 和 timeout 控制是否阻塞。
  • get(block=True, timeout=None):从队列中取出一个 item。block 和 timeout 控制是否阻塞。
  • task_done():标记一个任务已经完成。必须在 get() 后调用。
  • join():等待队列中所有任务完成。


这些队列类型和方法可以在多线程程序中有效地管理数据交换。

四、代码

queue 模块的一些主要类型和用法


相关文章
|
17天前
|
XML Shell API
python ConfigParser、shutil、subprocess、ElementTree模块简解
python ConfigParser、shutil、subprocess、ElementTree模块简解
|
16天前
|
存储 算法 数据库
使用python hashlib模块给明文字符串加密,以及如何撞库破解密码
`hashlib` 是 Python 中用于实现哈希功能的模块,它可以将任意长度的输入通过哈希算法转换为固定长度的输出,即散列值。该模块主要用于字符串加密,例如将用户名和密码转换为不可逆的散列值存储,从而提高安全性。`hashlib` 提供了多种哈希算法,如 `md5`、`sha1`、`sha256` 等。
32 1
|
4天前
|
Java Serverless Python
探索Python中的并发编程与`concurrent.futures`模块
探索Python中的并发编程与`concurrent.futures`模块
11 4
|
16天前
|
API Python
python ratelimit模块
python ratelimit模块
|
16天前
|
Python
像导入Python模块一样导入ipynb文件
像导入Python模块一样导入ipynb文件
|
17天前
|
Python
如何最简单、通俗地理解Python模块?
如何最简单、通俗地理解Python模块?
|
16天前
|
算法 Python
python tarfile模块
python tarfile模块
|
17天前
|
SQL 关系型数据库 MySQL
Python之MySQL操作及Paramiko模块操作
Python之MySQL操作及Paramiko模块操作
|
17天前
|
存储 JSON JavaScript
python序列化: json & pickle & shelve 模块
python序列化: json & pickle & shelve 模块
|
16天前
|
Python
如何在 Python 中导入模块
【8月更文挑战第29天】
19 1