Hi,
Even if you're not familiar with python, I don't think you would have any trouble understanding this code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from time import sleep
def __init__(self, parent):
def run(self):
self.stopped = False
self.connect(self.timer, SIGNAL("timeout()"), self.timeout)
self.timer.start(200)
self.exec_()
def stop(self):
self.stopped = True
def timeout(self):
if not self.stopped:
sleep(0.4) #400 miliseconds
#Here's actually a piece of code which I can't estimate
#deterministically how long it takes.
else:
self.timer.stop()
def __init__(self, parent = None):
self.resize(352, 288)
self.thread = Thread(self)
self.thread.start()
def closeEvent(self, event):
self.thread.stop()
self.thread.quit()
event.accept()
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
from time import sleep
class Thread(QThread):
def __init__(self, parent):
QThread.__init__(self, parent)
self.lock = QReadWriteLock()
def run(self):
self.stopped = False
self.timer = QTimer()
self.connect(self.timer, SIGNAL("timeout()"), self.timeout)
self.timer.start(200)
self.exec_()
def stop(self):
with QWriteLocker(self.lock):
self.stopped = True
def timeout(self):
with QReadLocker(self.lock):
if not self.stopped:
sleep(0.4) #400 miliseconds
#Here's actually a piece of code which I can't estimate
#deterministically how long it takes.
else:
self.timer.stop()
class Form(QDialog):
def __init__(self, parent = None):
QDialog.__init__(self, parent)
self.resize(352, 288)
self.thread = Thread(self)
self.thread.start()
def closeEvent(self, event):
self.thread.stop()
self.thread.quit()
event.accept()
To copy to clipboard, switch view to plain text mode
The problem is that, even the time-consuming procedure is placed in a thread other than the main (GUI) thread, the program will be frozen after 3-4 seconds in runtime and then you have to go straight to the task manager in order to close it!
As you might have guessed, there's something wrong in this code:
~~> self.timer.start(200)
################
~~> sleep(0.4)
~~> self.timer.start(200)
################
~~> sleep(0.4)
To copy to clipboard, switch view to plain text mode
Yeah, the timeout method takes at least 400 milliseconds to run, while we set the interval to 200 milliseconds. Well, I'm not a masochistic kind of person! Actually there is a snippet of code in the timeout method which does the following tasks:
1- Acquiring a frame from a web cam through OpenCV
2- Invoking face detection procedure
3- Calling a C-implemented function to convert IplImage to a format suitable for QImage.
This operation should be run periodically. I can't guess the time it needs to accomplish these tasks while it depends on many factors, like system load and the type of platform. And it may take longer than the period I set as the interval of QTimer.
Yeah I know that I should take another approach, but why the operation which is running in the thread, affects the main thread? Why does it slow down the main thread? And if it was supposed to behave so, what's the point of having a thread?!
Oh, btw, The following is part of 'Programming with Qt' by Matthias Kalle Dalheimer which stands that it's not a good idea to instantiate the QTimer class in any thread other than the main thread which I've found very common among Qt programmers:
http://books.google.com/books?id=1-Q...age&q=&f=false
* Timers and their events always belong in the GUI thread.
* Never create any objects of subclasses of QWidget, QTimer and QSocketNotifier in anything other than the main thread – not even when Qt is locked. Some Operating Systems will allocate the memory for these objects in a way that makes them inaccessible from the other threads.
Sorry for my bad english
Bookmarks