P
P
pypyshka2016-09-21 11:49:02
Python
pypyshka, 2016-09-21 11:49:02

What is the correct way to stop a QThread thread?

Good afternoon.
There is a program written in Python 3.4.4 using PyQt 4.11.4 that parses the site every 10 minutes. When the program starts, the connection to the site is checked check_connect(): if there is access, then status_connect = 1 and the thread is started. Every 10 minutes, the connection to the site is also checked. The problem is that if the site becomes unavailable after a while and the get_docs(sc) function works, the program will simply crash. As I understand it, this happens for the reason that the thread continues to work and you need to stop or terminate it. I tried to add check_docs.exit() to TimeoutError and ConnectionError exceptions. Maybe the reason is not in the flow at all. Please advise what can be done in this case?

def check_connect():
    global status_connect
    try:
        UTM_connect = HTTPConnection("test.ru")
        UTM_connect.request("GET", "/")
        UTM_connect.close()
    except TimeoutError:
        status_connect = 0
    except ConnectionError:
        status_connect = 0       
    else:
        status_connect = 1

def get_docs(sc):
    check_connect()
    if status_connect == 1:
        '''
        парсинг сайта

        '''
       s.enter(600, 1, get_docs, (sc,))

class check_docs_cl(QtCore.QThread):
    def __init__(self, parent = None):
        QtCore.QThread.__init__(self, parent)
    
    def run(self):
        global s       
        s = sched.scheduler(time.time, time.sleep)
        s.enter(10, 1, get_docs, (s,))
        s.run()

class main_cl(QtGui.QMainWindow):
    def __init__(self):
        super().__init__()         
            uic.loadUi("main.ui", self)       

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_window = main_cl()
    check_docs =  check_docs_cl()
    check_connect()
    if status_connect == 1:
        check_docs.start()
    sys.exit(app.exec_())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey6661313, 2016-09-21
@pypyshka

* in a teacher's tone:
If check_docs is executed once, and the functions in it work exclusively with global variables, and it contains only one function, and you start another thread (sched.scheduler) in it, why the hell do you need QThread??? and in general, why in the inside of get_docs try to restart the timer that caused it? because your get_docs will remain in the memory stack, and after an ebalion of cycles, it will simply display a recursion error, because it will endlessly wait for commands in the command cycle (s.enter -> s.enter -> s.enter -> s.enter and etc.) suddenly run out... And why don't you write us the output of your error to the console?
*in a normal voice:
ok now seriously - since you are using pyqt, use it to the fullest!:

class МойПервыйТаймерНаQt(QtCore.QObject):
    def __init__(self):
        super().__init__()
        self.basictimer = QtCore.QBasicTimer()
        self.basictimer.start(2000, self) # задержка в миллисекундах. 2000 для примера потому что 10 сек мне долго.

    def timerEvent(self, QTimerEvent):
        get_docs(self)  # вы конечно можете создать обьект sched.scheduler и передавать его, но зачем?


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    main_window = main_cl()
    мой_первый_таймер_на_Qt = МойПервыйТаймерНаQt()
    sys.exit(app.exec_())

and remove the line "s.enter(600, 1, get_docs, (sc,))" from get_docs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question