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!!
#!/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()
def __init__(self, parent=None):
startButton
= QPushButton('Start long (3 seconds) operation',self
) vbox.addWidget(startButton)
vbox.addWidget(label2)
self.setCentralWidget(centralwidget)
centralwidget.setLayout(vbox)
self.testObj = BcProcessing()
self.testObj.moveToThread(self.newThread)
self.newThread.started.connect(self.testObj.outputAsterisks)
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__':
print 'asdf', app.aboutQt()
window = MainWindow()
window.show()
sys.exit(app.exec_())
#!/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.outputAsterisks)
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_())
To copy to clipboard, switch view to plain text mode
Bookmarks