multithreading - Python PyQt4 signal not firing connected method -
i decided use signals indicate completion of thread. wired signal code restarts process if more work still exists. alas, code connected signal never fires. makes me sad.
i need know why method 'tester' in code block below doesn't fire.
i'm new python when started writing researched signals , modelled code after solution tgray posted here , apparently missed important, can't see is.
here massively simplified version of code:
from pyqt4.qtcore import * import time class worker(qthread): __pyqtsignals__ = ( "a", "b" ) def __init__(self): qthread.__init__(self, none) def run(self): print "'worker.run' starts" self.emit(signal("a")) print "'worker.run' ends" class main: def do(self): print "'do' starts" self.launch() time.sleep(2) print "'do' ends" def launch(self): print "'launch' starts" self.worker = worker() qobject.connect(self.worker, signal("a"), self.tester) self.worker.daemon = true self.worker.start() print "'launch' ends" def tester(self): print "tester fired!!" m=main()
running:
>>> m.do()
yields:
'do' starts 'launch' starts 'launch' ends 'do' ends 'worker.run' starts 'worker.run' ends
but not expected:
tester fired!!
what did miss? also, why prints in "worker.run" appear after else sleep(2)
?
if want see callback being called, have change way connect qthread signal slot:
qtcore.qobject.connect(self.worker, qt.signal("a"), self.tester, qt.qt.directconnection) #this added
explanation: qt signals/slots thread safe since qt 4. documentation says normal behaviour when connecting signal slot 'autoconnection', means direct (callback fired after emit) if both signal , slot in same thread, whereas queued if signal emitted thread (your case).
then, has process queue: normally, done qt loop while application running.
the fact signal "queued" means control transferred qt loop, make sure callback called in main thread if signal comes thread.
in question's code, there no event loop @ - way of getting slots called thread change way signals dispatched. don't use in code, though, since in normal application have qt loop running, have been dispatched (and safely threading issues).
Comments
Post a Comment