I have a simple GUI app where clicking a Start button, the start button gets disabled, a few asterisks get printed, then at the end, start button gets re-enabled.

After threaded function completes, I should see this:
finished.png

But maybe 3/5 times, it will just stay stuck here forever:
python_error.png

This intermittent issue only happens in Linux (CentOs 6.7). Windows is fine. Anyone have any ideas, this is so weird. Thanks!!


Qt Code:
  1. #!/usr/bin/env python2
  2.  
  3. import sys, time
  4. from PySide.QtGui import *
  5. from PySide import QtCore
  6.  
  7. class BcProcessing(QtCore.QObject):
  8. finished = QtCore.Signal()
  9.  
  10. def __init__(self, parent = None):
  11. super(BcProcessing, self).__init__(parent)
  12. self._isRunning = True
  13.  
  14. def outputAsterisks(self):
  15. end = time.time()+3
  16. self._isRunning = True
  17. while self._isRunning == True:
  18. sys.stdout.write('*')
  19. sys.stdout.flush()
  20. time.sleep(1)
  21. now = time.time()
  22. if now>=end:
  23. self._isRunning=False
  24. self.finished.emit()
  25.  
  26. class MainWindow(QMainWindow):
  27. def __init__(self, parent=None):
  28. QMainWindow.__init__(self,parent)
  29. centralwidget = QWidget(self)
  30. startButton = QPushButton('Start long (3 seconds) operation',self)
  31. label2 = QLabel('Hello')
  32. vbox = QVBoxLayout()
  33. vbox.addWidget(startButton)
  34. vbox.addWidget(label2)
  35. self.setCentralWidget(centralwidget)
  36. centralwidget.setLayout(vbox)
  37.  
  38. self.testObj = BcProcessing()
  39. self.newThread = QtCore.QThread()
  40. self.testObj.moveToThread(self.newThread)
  41. self.newThread.started.connect(self.testObj.outputAsterisks)
  42.  
  43. def closingThread():
  44. if self.newThread.isRunning():
  45. print 'closing running threads...'
  46. self.newThread.quit()
  47. self.newThread.wait()
  48.  
  49. def LongOperationStart():
  50. label2.setText('app is running...')
  51. startButton.setEnabled(False)
  52. self.newThread.start()
  53.  
  54. #Sometimes this does not get called in Linux!
  55. def LongOperationFinish():
  56. label2.setText('app finished')
  57. startButton.setEnabled(True)
  58. closingThread()
  59.  
  60. #GUI start button
  61. startButton.clicked.connect(LongOperationStart)
  62.  
  63. self.testObj.finished.connect(LongOperationFinish)
  64.  
  65. #On GUI window exiting, clean up threads
  66. app.aboutToQuit.connect(closingThread)
  67.  
  68.  
  69. if __name__=='__main__':
  70. app = QApplication(sys.argv)
  71. print 'asdf', app.aboutQt()
  72. window = MainWindow()
  73. window.show()
  74. sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode