【问题标题】:How to stop a thread from a GUI in python?如何从python中的GUI停止线程?
【发布时间】:2015-04-23 14:15:12
【问题描述】:

一个 gui 有一个启动按钮,它在线程中启动一个爬虫,以便 gui 可以更新它的发现。如何让我的停止按钮停止抓取线程?

class Scrape:
    def __init__(self):
        #commence scraping the web

class Gui:

    def __init__(self):
        super(Gui, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        #events
        self.ui.start.clicked.connect(self.start_scraper)
        self.ui.stop.clicked.connect(self.stop_scraper)

    def start_scraper(self):
        self.browse = threading.Thread(target=Scrape)

    def stop_bot(self) -> None:
        raise SystemExit

【问题讨论】:

  • 虽然该线程不处理 GUI...
  • 听起来你需要让你的工作线程(scraper)监听消息队列。所以你的gui线程可以告诉它退出。这篇文章可能对eli.thegreenplace.net/2011/12/27/…有帮助。抱歉,我没有时间制定答案。
  • 保罗,没问题。非常感谢任何类型的帮助,无论它是否只是指向可能的解决方案。谢谢

标签: python multithreading user-interface selenium-webdriver pyqt


【解决方案1】:

就我自己而言,我更喜欢下面这种方式,使用队列作为跨线程通信的通道,线程安全且易于使用。这都是异步通信。

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from Queue import Queue
from datetime import datetime


class MyThread(QThread):

    STOP_JOB_FLAG = None

    signal_thread_stop = pyqtSignal()

    def __init__(self, parent=None):
        super(MyThread, self).__init__(parent)
        self.mQueue = Queue()

    def startRun(self):
        if not self.isRunning():
            self.start()
            self.mQueue.put(datetime.now())
        else:
            self.mQueue.put(datetime.now())
            return

    def stopRun(self):
        if not self.isRunning():
            return
        else:
            self.mQueue.put(MyThread.STOP_JOB_FLAG)

    def run(self):
        while True:
            job = self.mQueue.get()
            if not job:
                qDebug("Thread run got None flag, will quit")
                self.signal_thread_stop.emit()
                break
            else:
                print "run " + str(job)
            self.mQueue.task_done()


class MyWidget(QWidget):
    def __init__(self, parent=None):
        super(MyWidget, self).__init__(parent)
        self.mLayout = QHBoxLayout(self)
        self.mStartBtn = QToolButton(self)
        self.mStopBtn = QToolButton(self)
        self.mStartBtn.setText("start")
        self.mStopBtn.setText("stop")
        self.setLayout(self.mLayout)
        self.mLayout.addWidget(self.mStartBtn)
        self.mLayout.addWidget(self.mStopBtn)
        self.mWorkerThread = MyThread()
        self.mStartBtn.released.connect(self.mWorkerThread.startRun)
        self.mStopBtn.released.connect(self.mWorkerThread.stopRun)
        self.mWorkerThread.signal_thread_stop.connect(self.handleRunFinished)

    def handleRunFinished(self):
        qDebug("handleRunFinished thread finished")

if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = MyWidget()
    w.show()
    app.exec_()

【讨论】:

    猜你喜欢
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 1970-01-01
    相关资源
    最近更新 更多