how to terminate qthread in python

Mohamed Yousof

I have GUI application which uses qwebview to make web automation process through long loop so I used QThread to do this but I can't terminate the thread, my code is below

class Main(QMainWindow):
    def btStart(self):
        self.mythread = BrowserThread()
        self.connect(self.mythread, SIGNAL('loop()'), self.campaign_loop, Qt.AutoConnection)
        self.mythread.start()

    def btStop(self):
        self.mythread.terminate()

    def campaign_loop(self):
        loop goes here

class BrowserThread(QThread):
    def __init__(self):
        QThread.__init__(self)

    def run(self):
        self.emit(SIGNAL('loop()'))

this code is working fine in starting thread but fail to stop the loop and browser still running even if I call close event to it and it disappears from GUI

Mohamed Yousof

the point is to make the main loop in the " run " method because " terminate " function is stopping the loop in " run " not the thread its self here is a working example for this, but unfortunately it works on windows only

import sys
import time
from PySide.QtGui import *
from PySide.QtCore import *

class frmMain(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        self.btStart = QPushButton('Start')
        self.btStop = QPushButton('Stop')
        self.counter = QSpinBox()
        self.layout = QVBoxLayout()
        self.layout.addWidget(self.btStart)
        self.layout.addWidget(self.btStop)
        self.layout.addWidget(self.counter)
        self.setLayout(self.layout)
        self.btStart.clicked.connect(self.start_thread)
        self.btStop.clicked.connect(self.stop_thread)

    def stop_thread(self):
        self.th.stop()

    def loopfunction(self, x):
        self.counter.setValue(x)

    def start_thread(self):
        self.th = thread(2)
        #self.connect(self.th, SIGNAL('loop()'), lambda x=2: self.loopfunction(x), Qt.AutoConnection)
        self.th.loop.connect(self.loopfunction)
        self.th.setTerminationEnabled(True)
        self.th.start()

class thread(QThread):
    loop = Signal(object)

    def __init__(self, x):
        QThread.__init__(self)
        self.x = x

    def run(self):
        for i in range(100):
            self.x = i
            self.loop.emit(self.x)
            time.sleep(0.5)

    def stop(self):
        self.terminate()


app = QApplication(sys.argv)
win = frmMain()

win.show()
sys.exit(app.exec_())

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

QThread在Python中的使用

来自分类Dev

How to terminate loop gracefully when CTRL+C was pressed in python

来自分类Dev

Python PyQT / PySide QThread限制

来自分类Dev

如何在python中终止qthread

来自分类Dev

QThread:使用GUI python进行线程化

来自分类Dev

在Python中同时运行多个qthread

来自分类Dev

Python - PyQt:在 QThread 完成后继续

来自分类Dev

QThread:当线程仍在Python中运行时被破坏

来自分类Dev

具有QThread和线程模块的Python多线程

来自分类Dev

python3 multiprocess.pool.terminate()不会杀死进程

来自分类Dev

Qthread,为qthread添加功能

来自分类Dev

如何在 Python PyQt 中的 QThread 上中断脚本执行?

来自分类Dev

Pyqt5 中的 QThreads:这是官方 QThread 文档的正确 C++ 到 Python 翻译吗?

来自分类Dev

boost python error message:terminate called after throwing an instance of 'boost::python::error_already_set'

来自分类Dev

引发'boost :: python :: error_already_set'实例后调用boost python错误消息:terminate

来自分类Dev

Python子进程Popen.terminate()仍然停留在wait()上

来自分类Dev

QThread和QML锁定

来自分类Dev

QThread的复杂用法-

来自分类Dev

从QThread返回QSqlQuery结果

来自分类Dev

QThread没有完成

来自分类Dev

PyQt,QThread,GIL,GUI

来自分类Dev

如何停止线程-Qthread

来自分类Dev

QThread中的精确间隔

来自分类Dev

如何从qml启动Qthread?

来自分类Dev

是否可以命名QThread?

来自分类Dev

暂停并恢复QThread

来自分类Dev

QThread :: currentThread()与QObject :: thread()

来自分类Dev

中断QThread睡眠

来自分类Dev

QThread中的精确间隔