How run two different threads simultaneously in pyqt

后端 未结 1 1252
感动是毒
感动是毒 2020-12-28 11:14

I have a situation where i need to start two threads one after the other. I tried the following code snippet, here i could able to start the Thread12 but not able to start T

相关标签:
1条回答
  • 2020-12-28 11:43

    You will be using QRunnable and QThreadPool, something like this:

    import sys
    
    from PyQt4.QtGui import QApplication
    from PyQt4.QtCore import QThreadPool, QObject, QRunnable, pyqtSignal
    
    class WorkerSignals(QObject):
        result = pyqtSignal(int)
    
    class Worker(QRunnable):
        def __init__(self, task):
            super(Worker, self).__init__()
    
            self.task = task
            self.signals = WorkerSignals()
    
        def run(self):
            print 'Sending', self.task
            self.signals.result.emit(self.task)
    
    class Tasks(QObject):
        def __init__(self):
            super(Tasks, self).__init__()
    
            self.pool = QThreadPool()
            self.pool.setMaxThreadCount(1)
    
        def process_result(self, task):
            print 'Receiving', task
    
        def start(self):
            for task in range(10):
                worker = Worker(task)
                worker.signals.result.connect(self.process_result)
    
                self.pool.start(worker)
    
            self.pool.waitForDone()
    
    if __name__ == "__main__":
        import  sys
    
        app = QApplication(sys.argv)
        main = Tasks()
        main.start()
        sys.exit(app.exec_())
    
    0 讨论(0)
提交回复
热议问题