[python] Jak ladit QThreads z PySide nebo PyQt ?

darkanry richard.andrysek na freenet.de
Neděle Leden 13 23:34:19 CET 2013


Stahl jsem si jednoduchy priklad pro praci s QThread. Priklad funguje bez problemu, tak jsem ho zacal upravovat. Bohuzel jsem zjistil, ze to v PyDev nejde ladit. Nevite nekdo jak na to? Kdyz to nejde s PyDev, jde to necim jinym?

Priklad:

#import pydevd
#pydevd.connected = True
#pydevd.settrace()
   
from PySide import QtCore

import time, sys

class MyWorkerThread(QtCore.QThread):
    message = QtCore.Signal(str)

    def __init__(self, id, parent=None):
        super(MyWorkerThread, self).__init__(parent)
        self.id = id

    def run(self):
        for i in range(10):
            self.message.emit("%d: %d" % (self.id, i))
            time.sleep(0.2)
            
class MainProgram():
    def __init__(self, parent=None):
        self.threads = []

        self.addWorker(MyWorkerThread(1))
        self.addWorker(MyWorkerThread(2))

    def addWorker(self, worker):
        worker.message.connect(self.printMessage, QtCore.Qt.QueuedConnection)
        # connect the finished signal to method so that we are notified
        worker.finished.connect(self.workersFinished)
        self.threads.append(worker)

    def startWorkers(self):
        for worker in self.threads:
            print worker.id
            worker.start()
            # no wait, no finished. you start the threads and leave.

    def workersFinished(self):
        if all(worker.isFinished() for worker in self.threads):
            # wait until all the threads finished
            QtCore.QCoreApplication.instance().quit()

    @QtCore.Slot(str)
    def printMessage(self, text):
        sys.stdout.write(text+'\n')
        sys.stdout.flush()

if __name__ == '__main__':
    
    app = QtCore.QCoreApplication(sys.argv)
    m = MainProgram()
    m.startWorkers()
    sys.exit(app.exec_())


Další informace o konferenci Python