清除工作线程pyqt5 python中的线程

时间:2019-12-03 05:53:05

标签: python pyqt5 threadpool

当我调用函数时,我使用了这个工作线程

class WorkerSignals(QObject):
    finished = pyqtSignals()

class Worker(QRunnable):
    def __init__(self, fn, *args, **kwargs):
        self.fn = fn
        self.args = args
        self.kwargs = kwargs
        self.signals = WorkerSignals()

    @pyqtSlot()
    def run(self):
        done = False
        try:
            self.fn(*self.args,**self.kwargs)
            done = True
        except Exception as e:
            print(str(e))
            done = False
        finally:
            if done == True:
                self.signals.finished.emit()

在我的主窗口中

self.threadpool = QThreadpool()
self.btn1.clicked.connect(self.first_func)
self.btn2.clicked.connect(self.second_func)

def first_func(self):
    worker = Worker(self.someother_func)
    self.threadpool.start(worker)
    worker.signals.finished.connect(self.another_func)

def second_func(self):
    worker = Worker(self.someother_func2)
    self.threadpool.start(worker)
    worker.signals.finished.connect(self.another_func2)

现在,当我单击btn1且检索数据花费的时间太长时,有时在进程仍在处理时单击btn2,我想停止或终止第一个进程或任何仍在运行的进程,以便仅流程2将会发生。我该怎么办?

0 个答案:

没有答案