Skip to content

Commit bda63d8

Browse files
committed
线程退出
1 parent d3f4e8f commit bda63d8

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

QThread/QuitThread.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2020/11/27
6+
@author: Irony
7+
@site: https://pyqt.site https://github.com/PyQt5
8+
9+
@file: QuitThread
10+
@description:
11+
"""
12+
13+
__Author__ = 'Irony'
14+
__Copyright__ = 'Copyright (c) 2020'
15+
__Version__ = 'Version 1.0'
16+
17+
import sys
18+
from time import time
19+
20+
from PyQt5.QtCore import QThread, QCoreApplication, QTimer
21+
22+
23+
class Thread(QThread):
24+
25+
def run(self):
26+
print('thread id', int(QThread.currentThreadId()))
27+
i = 0
28+
while i < 101 and not self.isInterruptionRequested():
29+
print('value', i, time())
30+
i += 1
31+
QThread.msleep(500)
32+
print('thread quit')
33+
34+
35+
if __name__ == '__main__':
36+
app = QCoreApplication(sys.argv)
37+
t = Thread()
38+
t.finished.connect(app.quit)
39+
t.start()
40+
# 3秒后退出
41+
print('will quit 3s latter')
42+
QTimer.singleShot(3000, t.requestInterruption)
43+
sys.exit(app.exec_())

QThread/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [moveToThread](#2moveToThread)
66
- [线程挂起恢复](#3线程挂起恢复)
77
- [线程休眠唤醒](#4线程休眠唤醒)
8+
- [线程退出](#5线程退出)
89

910
## 1、继承QThread
1011
[运行 InheritQThread.py](InheritQThread.py)
@@ -34,4 +35,11 @@
3435

3536
使用 `QWaitCondition``wait``wakeAll` 方法
3637

37-
![WakeupThread](ScreenShot/WakeupThread.gif)
38+
![WakeupThread](ScreenShot/WakeupThread.gif)
39+
40+
## 5、线程退出
41+
[运行 QuitThread.py](QuitThread.py)
42+
43+
`isInterruptionRequested``requestInterruption` 函数作为退出标识调用
44+
45+
![QuitThread](ScreenShot/QuitThread.jpg)

QThread/ScreenShot/QuitThread.jpg

27.6 KB
Loading

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分
148148
- [moveToThread](QThread/moveToThread.py)
149149
- [线程挂起恢复](QThread/SuspendThread.py)
150150
- [线程休眠唤醒](QThread/WakeupThread.py)
151+
- [线程退出](QThread/QuitThread.py)
151152

152153
- [QtQuick](QtQuick)
153154
- [Flat样式](QtQuick/FlatStyle.py)

0 commit comments

Comments
 (0)