PDA

View Full Version : Simple Qthreaded GUI app runs fine in Windows, but sometimes freezes in Linux



bobbayribs
20th February 2016, 00:30
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:
11723

But maybe 3/5 times, it will just stay stuck here forever:
11722

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



#!/usr/bin/env python2

import sys, time
from PySide.QtGui import *
from PySide import QtCore

class BcProcessing(QtCore.QObject):
finished = QtCore.Signal()

def __init__(self, parent = None):
super(BcProcessing, self).__init__(parent)
self._isRunning = True

def outputAsterisks(self):
end = time.time()+3
self._isRunning = True
while self._isRunning == True:
sys.stdout.write('*')
sys.stdout.flush()
time.sleep(1)
now = time.time()
if now>=end:
self._isRunning=False
self.finished.emit()

class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self,parent)
centralwidget = QWidget(self)
startButton = QPushButton('Start long (3 seconds) operation',self)
label2 = QLabel('Hello')
vbox = QVBoxLayout()
vbox.addWidget(startButton)
vbox.addWidget(label2)
self.setCentralWidget(centralwidget)
centralwidget.setLayout(vbox)

self.testObj = BcProcessing()
self.newThread = QtCore.QThread()
self.testObj.moveToThread(self.newThread)
self.newThread.started.connect(self.testObj.output Asterisks)

def closingThread():
if self.newThread.isRunning():
print 'closing running threads...'
self.newThread.quit()
self.newThread.wait()

def LongOperationStart():
label2.setText('app is running...')
startButton.setEnabled(False)
self.newThread.start()

#Sometimes this does not get called in Linux!
def LongOperationFinish():
label2.setText('app finished')
startButton.setEnabled(True)
closingThread()

#GUI start button
startButton.clicked.connect(LongOperationStart)

self.testObj.finished.connect(LongOperationFinish)

#On GUI window exiting, clean up threads
app.aboutToQuit.connect(closingThread)


if __name__=='__main__':
app = QApplication(sys.argv)
print 'asdf', app.aboutQt()
window = MainWindow()
window.show()
sys.exit(app.exec_())