线程退出

This commit is contained in:
Irony 2020-11-27 11:33:28 +08:00
parent d3f4e8f815
commit bda63d85a9
4 changed files with 53 additions and 1 deletions

43
QThread/QuitThread.py Normal file
View file

@ -0,0 +1,43 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2020/11/27
@author: Irony
@site: https://pyqt.site https://github.com/PyQt5
@email: 892768447@qq.com
@file: QuitThread
@description:
"""
__Author__ = 'Irony'
__Copyright__ = 'Copyright (c) 2020'
__Version__ = 'Version 1.0'
import sys
from time import time
from PyQt5.QtCore import QThread, QCoreApplication, QTimer
class Thread(QThread):
def run(self):
print('thread id', int(QThread.currentThreadId()))
i = 0
while i < 101 and not self.isInterruptionRequested():
print('value', i, time())
i += 1
QThread.msleep(500)
print('thread quit')
if __name__ == '__main__':
app = QCoreApplication(sys.argv)
t = Thread()
t.finished.connect(app.quit)
t.start()
# 3秒后退出
print('will quit 3s latter')
QTimer.singleShot(3000, t.requestInterruption)
sys.exit(app.exec_())

View file

@ -5,6 +5,7 @@
- [moveToThread](#2moveToThread)
- [线程挂起恢复](#3线程挂起恢复)
- [线程休眠唤醒](#4线程休眠唤醒)
- [线程退出](#5线程退出)
## 1、继承QThread
[运行 InheritQThread.py](InheritQThread.py)
@ -35,3 +36,10 @@
使用 `QWaitCondition``wait``wakeAll` 方法
![WakeupThread](ScreenShot/WakeupThread.gif)
## 5、线程退出
[运行 QuitThread.py](QuitThread.py)
`isInterruptionRequested``requestInterruption` 函数作为退出标识调用
![QuitThread](ScreenShot/QuitThread.jpg)

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

View file

@ -148,6 +148,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站
- [moveToThread](QThread/moveToThread.py)
- [线程挂起恢复](QThread/SuspendThread.py)
- [线程休眠唤醒](QThread/WakeupThread.py)
- [线程退出](QThread/QuitThread.py)
- [QtQuick](QtQuick)
- [Flat样式](QtQuick/FlatStyle.py)