Using QThreadPool with QRunnable in PyQt4

前端 未结 1 1440
孤独总比滥情好
孤独总比滥情好 2021-01-21 18:37

Consider the following code snippet

class Worker(QtCore.QRunnable):
    def __init__(self):
        super(Worker, self).__init__()

    def run(self):
                 


        
相关标签:
1条回答
  • 2021-01-21 19:26

    Don't know what is wrong with the QGis initialization, but the following code works as expected in my machine:

    from PyQt4 import QtCore
    
    class Worker(QtCore.QRunnable):
        def __init__(self):
            super(Worker, self).__init__()
    
        def run(self):
            print('Running Worker')
    
    class Tasks(QtCore.QObject):
        def __init__(self):
            super(Tasks, self).__init__()
            self.pool = QtCore.QThreadPool.globalInstance()
            self.pool.setMaxThreadCount(2)
    
        def start(self):
            for task in range(3):
                worker = Worker()
                self.pool.start(worker)
            self.pool.waitForDone()
    
    if __name__ == '__main__':
        tasks = Tasks()
        tasks.start()
    

    It gives me the following output:

    Running Worker
    Running Worker
    Running Worker
    
    0 讨论(0)
提交回复
热议问题