PyQt 異步任務多線程的幾種方案示例詳解
更新時間:2025年01月22日 09:40:05 作者:戀戀西風
文章總結了多線程異步編程的不同方式,包括QThread、QThreadPool和concurrent.futures,分別適用于不同場景,本文結合實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧
多線程異步線程是我們常用的,如我們在執(zhí)行耗時操作,又不想卡用主程序 ;
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) # 發(fā)送信號 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_()
子線程中回調主線程函數執(zhí)行,在子線程;
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) # 發(fā)送信號 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
:適合輕量級、高并發(fā)任務。concurrent.futures
:簡單結合信號與槽機制使用線程池。
到此這篇關于PyQt 異步任務多線程的幾種方案示例詳解的文章就介紹到這了,更多相關PyQt 多線程內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
TensorFlow安裝及jupyter notebook配置方法
下面小編就為大家?guī)硪黄猅ensorFlow安裝及jupyter notebook配置方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09Python編程實現(xiàn)線性回歸和批量梯度下降法代碼實例
這篇文章主要介紹了Python編程實現(xiàn)線性回歸和批量梯度下降法代碼實例,具有一定借鑒價值,需要的朋友可以參考下2018-01-01python開發(fā)之thread實現(xiàn)布朗運動的方法
這篇文章主要介紹了python開發(fā)之thread實現(xiàn)布朗運動的方法,實例分析了Python基于多線程實現(xiàn)繪圖的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11linux系統(tǒng)使用python獲取cpu信息腳本分享
這篇文章主要介紹了linux系統(tǒng)使用python獲取cpu信息腳本,大家參考使用吧2014-01-01Python?ConfigParser庫輕松讀寫INI文件實例探究
這篇文章主要為大家介紹了Python?ConfigParser庫輕松讀寫INI文件實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01