Arnold Krille wrote:
On Sunday 05 April 2009 21:29:44 Hazen Babcock wrote:
I'd suggest expanding your MyThread class to something like this:
class MyThread(QtCore.QThread):
def __init__(self, parent = None):
QtCore.QThread.__init__(self, parent)
self.running = 1
def run(self):
n = 0
step = 1
while self.running:
n += step
print n
self.msleep(100)
def stop(self):
self.running = 0
You should also look at the QtCore.QMutex() class, which provides a way
to synchronize (via locking) between the thread process and other
processes.
Another reason while this will not work as expected is the Big Intepreter
Lock. Unless QThread releases the BIL in its *sleep-functions, the thread will
block the execution of the main-thread...
Arnold
Yeah that wasn't such a good suggestion. I actually tested this one and
it works as expected for me:
import sys
from PyQt4 import QtCore, QtGui
import time
class MyThread(QtCore.QThread):
def __init__(self, parent = None):
QtCore.QThread.__init__(self, parent)
self.alive = 1
self.running = 0
self.n = 0
def run(self):
while self.alive:
step = 1
n = 0
while self.running:
n += step
print n
self.msleep(100)
self.msleep(100)
def toggle(self):
if self.running:
self.running = 0
else:
self.running = 1
def stop(self):
self.alive = 0
class Gui(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QGroupBox.__init__(self, parent)
self.gcenter = QtGui.QPushButton("X", self)
self.gcenter.setAutoRepeat(True)
guiLayout = QtGui.QGridLayout()
guiLayout.addWidget(self.gcenter,1,0)
self.setLayout(guiLayout)
self.thread = MyThread()
self.thread.start()
self.connect(self.gcenter, QtCore.SIGNAL("clicked()"),
self.thread.toggle)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
gui = Gui()
gui.show()
sys.exit(app.exec_())
I assume that QThread must be releasing the GIL while sleeping.
-Hazen
_______________________________________________
PyQt mailing list [email protected]
http://www.riverbankcomputing.com/mailman/listinfo/pyqt