Skip to content Skip to sidebar Skip to footer

Using Qthreadpool With Qrunnable In Pyqt4

Consider the following code snippet class Worker(QtCore.QRunnable): def __init__(self): super(Worker, self).__init__() def run(self): print('Running Work

Solution 1:

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

from PyQt4 import QtCore

classWorker(QtCore.QRunnable):
    def__init__(self):
        super(Worker, self).__init__()

    defrun(self):
        print('Running Worker')

classTasks(QtCore.QObject):
    def__init__(self):
        super(Tasks, self).__init__()
        self.pool = QtCore.QThreadPool.globalInstance()
        self.pool.setMaxThreadCount(2)

    defstart(self):
        for task inrange(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

Post a Comment for "Using Qthreadpool With Qrunnable In Pyqt4"