简体   繁体   English

如何在 PyQt5 中停止线程

[英]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"我尝试寻找我的问题的解决方案,但我似乎无法找到正确的方法,在 GUI 没有“无响应”的情况下停止它

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.我正在使用threading.Threading()并且我尝试了 SO 上可用的内容,而.join()是唯一允许的版本。 but this makes the GUI freeze when the button that is connected to stop_client() is clicked但这会在单击连接到stop_client()的按钮时使 GUI 冻结

I really suggest going over to QThreads for use with PyQt5.我真的建议使用 QThreads 来与 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.以及能够更好地使用 PyQts 信号和插槽系统。

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.使用此代码,理想情况下,您的代码将在将 terminate 设置为 True 后最多停止一秒钟。 If client_run() stops code execution, then you might get a hitch however.如果 client_run() 停止代码执行,那么您可能会遇到问题。 You can also store username, password in the class init if needed in during the run.如果在运行期间需要,您还可以在类 init 中存储用户名、密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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