多线程异步线程是我们常用的,如我们在执行耗时操作,又不想卡用主程序 ;
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
:简单结合信号与槽机制使用线程池。
到此这篇关于pyqt 异步任务多线程的几种方案示例详解的文章就介绍到这了,更多相关pyqt 多线程内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论