PyQt 异步任务 多线程的几种方案

多线程异步线程是我们常用的,如我们在执行耗时操作,又不想卡用主程序 ;

1. QThread 

from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget, QPushButton
import time

class WorkerThread(QThread):
    progress = pyqtSignal(int)  # 定义信号
    def __init__(self,main_instance):
        QThread.__init__(self)
        self.main_instance = main_instance
    def run(self):
        for i in range(1, 101):
            self.main_instance.excuteSomeThing()
            self.progress.emit(i)  # 发送信号

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(800, 600)
        self.initUI()

    def initUI(self):
        self.label = QLabel("进度: 0")
        self.button = QPushButton("开始任务")
        self.button.clicked.connect(self.start_task)

        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.button)
        self.setLayout(layout)
    def excuteSomeThing(self):
        time.sleep(0.1)  # 模拟耗时操作

    def start_task(self):
        self.worker = WorkerThread(self)
        self.worker.progress.connect(self.update_label)  # 连接信号到槽函数
        self.worker.start()  # 启动线程

    def update_label(self, value):
        self.label.setText(f"进度: {value}")

app = QApplication([])
window = MainWindow()
window.show()
app.exec_()

子线程中回调主线程函数执行,在子线程;

2. QThreadPool

from PyQt5.QtCore import QRunnable, QThreadPool, pyqtSignal, QObject
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget, QPushButton
import time

class WorkerSignals(QObject):
    progress = pyqtSignal(int)

class Worker(QRunnable):
    def __init__(self):
        super().__init__()
        self.signals = WorkerSignals()

    def run(self):
        for i in range(1, 101):
            time.sleep(0.01)  # 模拟耗时操作
            self.signals.progress.emit(i)  # 发送信号

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(800, 600)
        self.initUI()
        self.thread_pool = QThreadPool()

    def initUI(self):
        self.label = QLabel("进度: 0")
        self.button = QPushButton("开始任务")
        self.button.clicked.connect(self.start_task)

        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.button)
        self.setLayout(layout)

    def start_task(self):
        worker = Worker()
        worker.signals.progress.connect(self.update_label)
        self.thread_pool.start(worker)

    def update_label(self, value):
        self.label.setText(f"进度: {value}")

app = QApplication([])
window = MainWindow()
window.show()
app.exec_()

3.concurrent

from PyQt5.QtCore import pyqtSignal, QObject
from PyQt5.QtWidgets import QApplication, QLabel, QVBoxLayout, QWidget, QPushButton
from concurrent.futures import ThreadPoolExecutor
import time

class Worker(QObject):
    progress = pyqtSignal(int)

    def do_work(self):
        for i in range(1, 101):
            time.sleep(0.021)  # 模拟耗时操作
            self.progress.emit(i)

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.resize(800, 600)
        self.initUI()
        self.executor = ThreadPoolExecutor(max_workers=10)

    def initUI(self):
        self.label = QLabel("进度: 0")
        self.button = QPushButton("开始任务")
        self.button.clicked.connect(self.start_task)

        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(self.button)
        self.setLayout(layout)

    def start_task(self):
        self.worker = Worker()
        self.worker.progress.connect(self.update_label)
        self.executor.submit(self.worker.do_work)

    def update_label(self, value):
        self.label.setText(f"进度: {value}")

app = QApplication([])
window = MainWindow()
window.show()
app.exec_()

总结

  • QThread:适合需要自定义线程逻辑的场景。
  • QRunnable + QThreadPool:适合轻量级、高并发任务。
  • concurrent.futures:简单结合信号与槽机制使用线程池。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋恋西风

up up up

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值