Python-PyQt中带有QThread的后台线程

发布于 2021-02-02 23:17:58

我有一个程序,可以通过我在PyQt中编写的gui与正在使用的无线电接口。显然,无线电的主要功能之一是传输数据,但要连续进行,我必须循环写入,这会导致gui挂起。由于我从未处理过线程,因此我尝试使用QCoreApplication.processEvents().“无线电”需要在两次传输之间睡眠的方法来消除这些挂起,因此gui仍根据这些睡眠持续的时间来挂起。

是否有使用QThread修复此问题的简单方法?我一直在寻找有关如何使用PyQt实现多线程的教程,但是其中大多数都涉及设置服务器,并且比我需要的要先进得多。老实说,我什至不真正需要我的线程在它运行时更新任何东西,我只需要启动它,使其在后台传输,然后停止它即可。

关注者
0
被浏览
116
1 个回答
  • 面试哥
    面试哥 2021-02-02
    为面试而生,有面试问题,就找面试哥。

    我创建了一个小示例,展示了3种不同的简单线程处理方式。我希望它能帮助你找到解决问题的正确方法。

    import sys
    import time
    
    from PyQt5.QtCore import (QCoreApplication, QObject, QRunnable, QThread,
                              QThreadPool, pyqtSignal)
    
    
    # Subclassing QThread
    # http://qt-project.org/doc/latest/qthread.html
    class AThread(QThread):
    
        def run(self):
            count = 0
            while count < 5:
                time.sleep(1)
                print("A Increasing")
                count += 1
    
    # Subclassing QObject and using moveToThread
    # http://blog.qt.digia.com/blog/2007/07/05/qthreads-no-longer-abstract
    class SomeObject(QObject):
    
        finished = pyqtSignal()
    
        def long_running(self):
            count = 0
            while count < 5:
                time.sleep(1)
                print("B Increasing")
                count += 1
            self.finished.emit()
    
    # Using a QRunnable
    # http://qt-project.org/doc/latest/qthreadpool.html
    # Note that a QRunnable isn't a subclass of QObject and therefore does
    # not provide signals and slots.
    class Runnable(QRunnable):
    
        def run(self):
            count = 0
            app = QCoreApplication.instance()
            while count < 5:
                print("C Increasing")
                time.sleep(1)
                count += 1
            app.quit()
    
    
    def using_q_thread():
        app = QCoreApplication([])
        thread = AThread()
        thread.finished.connect(app.exit)
        thread.start()
        sys.exit(app.exec_())
    
    def using_move_to_thread():
        app = QCoreApplication([])
        objThread = QThread()
        obj = SomeObject()
        obj.moveToThread(objThread)
        obj.finished.connect(objThread.quit)
        objThread.started.connect(obj.long_running)
        objThread.finished.connect(app.exit)
        objThread.start()
        sys.exit(app.exec_())
    
    def using_q_runnable():
        app = QCoreApplication([])
        runnable = Runnable()
        QThreadPool.globalInstance().start(runnable)
        sys.exit(app.exec_())
    
    if __name__ == "__main__":
        #using_q_thread()
        #using_move_to_thread()
        using_q_runnable()
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看