我试图理解为什么这个Worker
线程故意使用相当大的处理量(尤其是对这些字典的排序(,导致GUI线程变得没有响应。这是一个MRE:
from PyQt5 import QtCore, QtWidgets
import sys, time, datetime, random
def time_print(msg):
ms_now = datetime.datetime.now().isoformat(sep=' ', timespec='milliseconds')
thread = QtCore.QThread.currentThread()
print(f'{thread}, {ms_now}: {msg}')
def dict_reorder(dictionary):
return {k: v for k, v in sorted(dictionary.items())}
class Sequence(object):
n_sequence = 0
simple_sequence_map = {}
sequence_to_sequence_map = {}
prev_seq = None
def __init__(self):
Sequence.n_sequence += 1
if Sequence.n_sequence % 1000 == 0:
print(f'created sequence {Sequence.n_sequence}')
rand_int = random.randrange(100000)
self.text = str(rand_int)
Sequence.simple_sequence_map[self] = rand_int
if Worker.stop_ordered:
time_print(f'init() A: stop ordered... stopping now')
return
dict_reorder(Sequence.simple_sequence_map)
if Sequence.prev_seq:
Sequence.sequence_to_sequence_map[self] = Sequence.prev_seq
if Worker.stop_ordered:
time_print(f'init() B: stop ordered... stopping now')
return
dict_reorder(Sequence.sequence_to_sequence_map)
Sequence.prev_seq = self
def __lt__(self, other):
return self.text < other.text
class WorkerSignals(QtCore.QObject):
progress = QtCore.pyqtSignal(int)
stop_me = QtCore.pyqtSignal()
class Worker(QtCore.QRunnable):
def __init__(self, *args, **kwargs):
super().__init__()
self.signals = WorkerSignals()
def stop_me_slot(self):
time_print('stop me slot')
Worker.stop_ordered = True
@QtCore.pyqtSlot()
def run(self):
total_n = 30000
Worker.stop_ordered = False
for n in range(total_n):
progress_pc = int(100 * float(n+1)/total_n)
self.signals.progress.emit(progress_pc)
Sequence()
if Worker.stop_ordered:
time_print(f'run(): stop ordered... stopping now, n {n}')
return
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
layout = QtWidgets.QVBoxLayout()
self.progress = QtWidgets.QProgressBar()
layout.addWidget(self.progress)
start_button = QtWidgets.QPushButton('Start')
start_button.pressed.connect(self.execute)
layout.addWidget(start_button)
self.stop_button = QtWidgets.QPushButton('Stop')
layout.addWidget(self.stop_button)
w = QtWidgets.QWidget()
w.setLayout(layout)
self.setCentralWidget(w)
self.show()
self.threadpool = QtCore.QThreadPool()
self.resize(800, 600)
def execute(self):
self.worker = Worker()
self.worker.signals.progress.connect(self.update_progress)
self.stop_button.pressed.connect(self.worker.stop_me_slot)
self.threadpool.start(self.worker)
def update_progress(self, progress):
self.progress.setValue(progress)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
app.exec_()
在我的机器上,直到大约12%,GUI都没有明显的响应:按钮不会获取它们的";悬停在";颜色(浅蓝色(并且似乎不能被点击(尽管点击"停止"确实会在许多秒后导致停止(。可怕的旋转器不时出现(W10操作系统中的蓝色圆圈(。
大约12%之后,就可以正常使用按钮了。
我做错了什么?
一个非常简单的解决方案是"睡眠;使用基本time.sleep
的线程:即使间隔很小,也会给主线程足够的时间来处理其事件队列,避免UI锁定:
def run(self):
total_n = 30000
Worker.stop_ordered = False
for n in range(total_n):
progress_pc = int(100 * float(n+1)/total_n)
self.signals.progress.emit(progress_pc)
Sequence()
if Worker.stop_ordered:
time_print(f'run(): stop ordered... stopping now, n {n}')
return
time.sleep(.0001)
注意:pyqtSlot
装饰器是无用的,因为它只适用于QObject子类(QRunnable不是(;你可以删除它。
Python不能运行多个CPU密集型线程。其原因是GIL。基本上,Python线程不适合做任何事情,只适合等待I/O。
如果您想要CPU密集型部件,请尝试使用Cython重写密集型部件或使用multiprocessing
,但来回发送数据的时间可能会很长。
无法复制,即使资源有限,ui也能保持响应。你试过在没有调试器的情况下运行它吗?
正如@9000所暗示的那样,GIL可能是个问题。
或者可能是充满progress
信号的事件循环,尝试为每个序列发出少于一个信号。
顺便说一句:若每次使用dict_reorder
都不丢弃排序结果,那个么程序的工作速度会更快。尝试替换
dict_reorder(Sequence.simple_sequence_map)
带有
Sequence.simple_sequence_map = dict_reorder(Sequence.simple_sequence_map)
和
dict_reorder(Sequence.sequence_to_sequence_map)
带有
Sequence.sequence_to_sequence_map = dict_reorder(Sequence.sequence_to_sequence_map)