1

I have in an application in python a worker thread that prevents the GUI from being frozen, since it requires continuous processing in the application. The worker thread calls the "doing" function on the main thread that often has heavy processing.

The problem is that when a heavy processing happens the thread does not wait for the code of the "doing" function to be executed and executes the function again.

Code:

import time
import sys
from PySide import QtCore, QtGui

class ServerThread(QtCore.QThread):

    def __init__(self, parent=None):
        QtCore.QThread.__init__(self)
        self.x = 0
        self.sleep_time = 1        

    def start_server(self):
        while True :
            print "\nBegin Thread: "+str(self.x)
            self.emit(QtCore.SIGNAL("dosomething(int)"), self.x)
            time.sleep(self.sleep_time)
            print "End Thread: "+str(self.x)
            self.x += 1

    def run(self):
        self.start_server()


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.thread = ServerThread()
        self.thread.start()
        self.connect(self.thread, QtCore.SIGNAL("dosomething(int)"), self.doing)

    def doing(self, x):
        print "--Begin Process: " + str(x)
        #Does some heavy processing
        h = 0
        for i in range(30000000) : 
            h = x ** 2
        print "--End Process: " + str(x)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    frame = MainWindow()
    frame.show()
    app.exec_()

I need the "doing" function to be executed only when the last one has been executed completely.

Image 1 shows the current output and picture 2 shows the expected output.

Current Output:

enter image description here

Expected output:

enter image description here

Surajano
  • 2,688
  • 13
  • 25
E.G. Cortes
  • 65
  • 12

0 Answers0