简体   繁体   中英

How to Stop a thread in PyQt5

I tried searching for solutions to my problem but I cant seem to find the right way, to stop it without the GUI getting a "not responding"

Basically heres how im starting and stopping it:

    def startTheThread(self):
        self.check_elements()
        # Create the new thread. The target function is 'myThread'. The
        # function we created in the beginning.




        _email = str(self.lineEdit.text())
        _password = str(self.lineEdit_2.text())
        captcha_api = str(self.lineEdit_5.text())
        print(betburg_email)
        print(betburg_password)
        print(captcha_api)
        print(mode)


        self.t = threading.Thread(name = 'myThread', target = myThread, args = (self.theCallbackFunc, _email, _password, captcha_api))
        self.t.start()




    def stop_client(self):
        self.check_elements()

        self.t.join()








 def theCallbackFunc(self, msg):
        print('the thread has sent this message to the GUI:')
        print(msg)
        print('---------')


class Communicate(QtCore.QObject):
    myGUI_signal = QtCore.pyqtSignal(str)



def myThread(callbackFunc, betburg_email, betburg_password, captcha_api):
    # Setup the signal-slot mechanism.
    mySrc = Communicate()
    mySrc.myGUI_signal.connect(callbackFunc) 

    # Endless loop. You typically want the thread
    # to run forever.
    while(True):
        # Do something useful here.
        client_run(betburg_email, betburg_password, captcha_api)

        msgForGui = 'Thread running...'
        mySrc.myGUI_signal.emit(msgForGui)

I tried starting another thread to handle the closing but also doesnt work. im using threading.Threading() and i tried whats available on SO, and .join() is the only allowed version. but this makes the GUI freeze when the button that is connected to stop_client() is clicked

I really suggest going over to QThreads for use with PyQt5. They have the added advantage of being able to be killed. As well as being able to work with PyQts signal and slot system a bit more nicely.

However, for using the regular threading module, you can subclass it and add a flag to toggle, that is checked when the thread is running and causes it to stop when changed. A very basic event loop.

import time
import threading


class MyThread(threading.Thread):
    def __init__(self, *args, **kwargs):
        super(MyThread, self).__init__(*args, **kwargs)
        self.terminate = False

    def run(self):  # This needs to the function name! Do your thing in this
        while not self.terminate:
            print('Do Stuff, state is ', self.terminate)
            time.sleep(1)  # As long as the "Do stuff" happens fast, it checks every one second


if __name__ == '__main__':
    t = MyThread()
    t.start() # Note, we use .start() but define what it does in the .run method. 
    time.sleep(5)
    t.terminate = True

Using this code, your code will ideally stop at most one second after setting terminate to True. If client_run() stops code execution, then you might get a hitch however. You can also store username, password in the class init if needed in during the run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM