Skip to main content

PySide6 高级线程工具库 - 带返回值的线程类和线程池执行器

Project description

QThreadWithReturn

QThreadWithReturn

Python PySide6 License Tests

基于 PySide6 的多线程高度封装库,简化 GUI 应用中的多线程编程。

简单易用,支持返回值和回调机制,避免复杂的信号槽设置、完善的内存回收、超时控制和任务取消功能、线程池支持、以及完整的类型提示。

简介

QThreadWithReturn 灵感来自日常的工作以及该库的前辈 qasync 库,由于 qasync 会有异步传染的问题,如果需要在以往的项目里面引入 qasync 会导致需要对现有代码进行大规模的修改。QThreadWithReturn 为了能够在旧项目里面渐进式地引入多线程编程,提供了一个类似于 concurrent.futures.Future 的 API,能够让您在不改变现有代码结构的情况下,轻松地改善多线程。

快速开始

下面是一个简单的银行取款的例子

"""
QThreadWithReturn的基础例子
"""

import time

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QLabel

from qthreadwithreturn import QThreadWithReturn


class Bank:
    """模拟银行取款操作

    这里是耗时操作的例子,在实际的项目中逻辑应该放在其他的模块中,不应该和界面代码混在一起
    这样做只是为了演示 QThreadWithReturn 的使用
    """

    def draw(self, amount: float) -> str:
        """模拟取款操作"""
        time.sleep(2)  # 模拟耗时操作
        return f"成功取款 {amount} 元"


class MyWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("QThreadWithReturn 示例")
        self.setGeometry(100, 100, 300, 200)

        self.bank = Bank()

        self.button = QPushButton("取款 100 元", self)
        self.button.setGeometry(50, 50, 200, 40)

        self.label = QLabel("等待取款...", self)
        self.label.setAlignment(Qt.AlignmentFlag.AlignHCenter)
        self.label.setGeometry(50, 100, 200, 40)

        self.button.clicked.connect(self.start_draw)

    def start_draw(self):
        """开始取款操作"""
        self.button.setEnabled(False)
        self.label.setText("取款中...")

        # 使用 QThreadWithReturn 进行取款操作
        # 通过finished和failure两个闭包函数节约了两个信号,运行完毕之后返回值会自动传入finished函数
        # 如果发生异常则会传入failure函数
        def finished(result: str):
            # 成功后自动调用(传入参数为self.bank.draw的返回值)
            self.label.setText(result)
            self.button.setEnabled(True)

        def failure(result: Exception):
            # 失败后自动调用(传入参数为self.bank.draw抛出的异常)
            self.label.setText(f"取款失败: {result}")
            self.button.setEnabled(True)

        thread = QThreadWithReturn(self.bank.draw, 100)  # 调用取款方法,传入参数100
        thread.add_done_callback(finished)
        thread.add_failure_callback(failure)
        thread.start()


if __name__ == '__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()

🚀 安装

# 使用 uv
uv add qthreadwithreturn

uv sync # 安装依赖

# 使用 pip  
pip install qthreadwithreturn
pip install PySide6 # 如果还没有安装 PySide6 的话(可选)

✨ 特性

🎯 QThreadWithReturn

  • concurrent.futures.Future 的 API,无需二次学习,快速上手
  • 内置超时控制和任务取消(包括强制停止)
  • 自动管理线程生命周期,防止内存泄漏
  • 支持任意可调用对象(函数、方法、lambda 等)
  • 完整的类型提示
  • 与 Qt 事件循环无缝集成

🏊‍♂️ QThreadPoolExecutor

  • concurrent.futures.ThreadPoolExecutor 的 API,无需二次学习,快速上手
  • 线程池管理和任务调度
  • 支持线程初始化器和命名
  • 支持 as_completed 方法按完成顺序处理任务
  • 任务取消和强制停止支持
  • 完整的类型提示
  • 上下文管理器支持

📖 API 参考

这里只给出公开方法的简要说明,完整的文档请参考具体的函数文档,在安装了 qthreadwithreturn 之后在 IDE 中悬浮就可以查看帮助

QThreadWithReturn

带返回值的 Qt 线程类,提供类似 concurrent.futures.Future 的 API。

方法 描述
start(timeout_ms: int = -1) 启动线程执行任务
result(timeout_ms: int = -1) 获取任务执行结果,阻塞等待
exception(timeout_ms: int = -1) 获取任务执行时抛出的异常
cancel(force_stop: bool = False) 取消线程执行
running() 检查任务是否正在运行
done() 检查任务是否已完成
cancelled() 检查任务是否被取消
wait(timeout_ms: int = -1, force_stop: bool = False) 等待任务完成
add_done_callback(callback: Callable) 添加任务成功完成后的回调函数
add_failure_callback(callback: Callable) 添加任务失败后的回调函数

QThreadPoolExecutor

线程池执行器,API 兼容 concurrent.futures.ThreadPoolExecutor

不建议使用 with 语句,因为在 GUI 应用中会导致 UI 阻塞。

静态方法

方法 返回类型 描述
as_completed(fs: Iterable["QThreadWithReturn"], timeout: float = -1) Iterator["QThreadWithReturn"] 返回一个迭代器,按完成顺序生成 Future 对象

实例方法

方法 描述
submit(fn: Callable, /, *args, **kwargs) 提交任务到线程池执行
shutdown(wait: bool = False, *, cancel_futures: bool = False, force_stop: bool = False) 关闭线程池
add_done_callback(callback: Callable) 添加池级别完成回调,当所有任务完成时执行
add_failure_callback(callback: Callable) 添加任务级别失败回调,当任何任务失败时执行
  • add_done_callback:当所有活跃任务完成且没有待处理任务时触发
  • add_failure_callback:每个失败任务都会触发一次

📄 许可证

本项目使用 MIT 许可证 - 查看 LICENSE 文件了解详情。

📞 支持

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qthreadwithreturn-1.6.19.tar.gz (70.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qthreadwithreturn-1.6.19-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file qthreadwithreturn-1.6.19.tar.gz.

File metadata

  • Download URL: qthreadwithreturn-1.6.19.tar.gz
  • Upload date:
  • Size: 70.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.3

File hashes

Hashes for qthreadwithreturn-1.6.19.tar.gz
Algorithm Hash digest
SHA256 ff566140ab8e3dd45eaee3c7959fd2a67201f0119682fb44937305fd417569ef
MD5 e015142f30a123c2b7d77f2341b9e1cd
BLAKE2b-256 8a7b23c979609781774e8dee59ddeee48616dd51c91f62dc738efc52dc376c60

See more details on using hashes here.

File details

Details for the file qthreadwithreturn-1.6.19-py3-none-any.whl.

File metadata

File hashes

Hashes for qthreadwithreturn-1.6.19-py3-none-any.whl
Algorithm Hash digest
SHA256 cd86ea17d84358953a0f796bcb0181d292027e129c19cc01e2b98c904dd34ed1
MD5 1bbd06e98d3b50352b51407741f03c2e
BLAKE2b-256 45342582f46b2adee97f90efeb0c62551f3a35f9aed7fd549c05a7d482cbf2ef

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page