PyQt Thread not running

783 Views Asked by At

I am trying to run a long running test suite as a background thread with PyQt. On osx the thread never runs, but on windows the thread runs if I include a print statement after starting the thread. The code looks like this:

def runModuleTests(self):
    self.stackedWidget.setCurrentIndex(2)

    self.testThread = QtCore.QThread()
    tr = TestRunner(self.on_testResult, self.moduleArticleNumber, self.moduleSerialNumber, self.testBedArticleNumber)
    tr.moveToThread(self.testThread)
    tr.testFinished.connect(self.testThread.quit)
    self.testThread.started.connect(tr.runModuleTests)
    self.testThread.start()
    print "This print out is here to make threads work.  UGLY HACK!"

I have these questions:

  • Is this print statement causing my GUI to yield on windows, so the other thread starts?
  • How can I get the thread to start on both mac osx and windows?
  • Does the GIL mean I cannot get a responsive UI using PyQt?
  • Am I doing things the wrong way?

I have taken inspiration from the answer to this question: Background thread with QThread in PyQt

1

There are 1 best solutions below

0
On

The comment from @Zaiborg was what I was missing. You need to keep a handle on the object even if you move it to the thread.