PyQt 異步任務(wù)多線(xiàn)程的幾種方案示例詳解
多線(xiàn)程異步線(xiàn)程是我們常用的,如我們?cè)趫?zhí)行耗時(shí)操作,又不想卡用主程序 ;
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) # 定義信號(hào)
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ā)送信號(hào)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(800, 600)
self.initUI()
def initUI(self):
self.label = QLabel("進(jìn)度: 0")
self.button = QPushButton("開(kāi)始任務(wù)")
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) # 模擬耗時(shí)操作
def start_task(self):
self.worker = WorkerThread(self)
self.worker.progress.connect(self.update_label) # 連接信號(hào)到槽函數(shù)
self.worker.start() # 啟動(dòng)線(xiàn)程
def update_label(self, value):
self.label.setText(f"進(jìn)度: {value}")
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()子線(xiàn)程中回調(diào)主線(xiàn)程函數(shù)執(zhí)行,在子線(xiàn)程;
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) # 模擬耗時(shí)操作
self.signals.progress.emit(i) # 發(fā)送信號(hào)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.resize(800, 600)
self.initUI()
self.thread_pool = QThreadPool()
def initUI(self):
self.label = QLabel("進(jìn)度: 0")
self.button = QPushButton("開(kāi)始任務(wù)")
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"進(jìn)度: {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) # 模擬耗時(shí)操作
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("進(jìn)度: 0")
self.button = QPushButton("開(kāi)始任務(wù)")
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"進(jìn)度: {value}")
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()總結(jié)
QThread:適合需要自定義線(xiàn)程邏輯的場(chǎng)景。QRunnable + QThreadPool:適合輕量級(jí)、高并發(fā)任務(wù)。concurrent.futures:簡(jiǎn)單結(jié)合信號(hào)與槽機(jī)制使用線(xiàn)程池。
到此這篇關(guān)于PyQt 異步任務(wù)多線(xiàn)程的幾種方案示例詳解的文章就介紹到這了,更多相關(guān)PyQt 多線(xiàn)程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 詳解PyQt5中Thread多線(xiàn)程的使用
- Python Pyqt5多線(xiàn)程更新UI代碼實(shí)例(防止界面卡死)
- 詳解PyQt5 GUI 接收UDP數(shù)據(jù)并動(dòng)態(tài)繪圖的過(guò)程(多線(xiàn)程間信號(hào)傳遞)
- Pyqt5 實(shí)現(xiàn)多線(xiàn)程文件搜索的案例
- PyQt5多線(xiàn)程防卡死和多窗口用法的實(shí)現(xiàn)
- python GUI庫(kù)圖形界面開(kāi)發(fā)之PyQt5多線(xiàn)程中信號(hào)與槽的詳細(xì)使用方法與實(shí)例
- 利用PyQt中的QThread類(lèi)實(shí)現(xiàn)多線(xiàn)程
- PyQt5中多線(xiàn)程模塊QThread使用方法的實(shí)現(xiàn)
- PYQT5開(kāi)啟多個(gè)線(xiàn)程和窗口,多線(xiàn)程與多窗口的交互實(shí)例
相關(guān)文章
TensorFlow安裝及jupyter notebook配置方法
下面小編就為大家?guī)?lái)一篇TensorFlow安裝及jupyter notebook配置方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
Python編程實(shí)現(xiàn)線(xiàn)性回歸和批量梯度下降法代碼實(shí)例
這篇文章主要介紹了Python編程實(shí)現(xiàn)線(xiàn)性回歸和批量梯度下降法代碼實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
python開(kāi)發(fā)之thread實(shí)現(xiàn)布朗運(yùn)動(dòng)的方法
這篇文章主要介紹了python開(kāi)發(fā)之thread實(shí)現(xiàn)布朗運(yùn)動(dòng)的方法,實(shí)例分析了Python基于多線(xiàn)程實(shí)現(xiàn)繪圖的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
linux系統(tǒng)使用python獲取cpu信息腳本分享
這篇文章主要介紹了linux系統(tǒng)使用python獲取cpu信息腳本,大家參考使用吧2014-01-01
python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解
這篇文章主要介紹了python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解,需要的朋友可以參考下2020-02-02
Python?ConfigParser庫(kù)輕松讀寫(xiě)INI文件實(shí)例探究
這篇文章主要為大家介紹了Python?ConfigParser庫(kù)輕松讀寫(xiě)INI文件實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Python性能調(diào)優(yōu)的十個(gè)小技巧總結(jié)
大家好,今天這篇文章關(guān)于Python性能調(diào)優(yōu)的10個(gè)小技巧,每天花5-10分鐘閱讀我的文章,對(duì)你技術(shù)提升一定會(huì)有幫助。喜歡記得收藏以防迷路2021-11-11
使用python實(shí)現(xiàn)畫(huà)AR模型時(shí)序圖
今天小編就為大家分享一篇使用python實(shí)現(xiàn)畫(huà)AR模型時(shí)序圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
使用Python實(shí)現(xiàn)畫(huà)一個(gè)中國(guó)地圖
今天小編就為大家分享一篇使用Python實(shí)現(xiàn)畫(huà)一個(gè)中國(guó)地圖,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
Python中用字符串調(diào)用函數(shù)或方法示例代碼
字符串作為python中常用的數(shù)據(jù)類(lèi)型,掌握字符串的常用方法十分必要。下面這篇文章主要給大家介紹了關(guān)于Python中通過(guò)字符串調(diào)用函數(shù)或方法的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-08-08

