[PyKDE] Correct cross-thread communication example?
Paul Giannaros
ceruleanblaze at gmail.com
Mon Jan 8 18:19:56 GMT 2007
No such luck. I think I mentioned in the source code I tried that. See the
given example:
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
from time import time
class B (QThread):
def __init__(self):
QThread.__init__(self)
p("Creating thread")
def bfunc(self):
self.sleep(3)
p("finished in b()")
def run(self):
self.exec_()
class A(QDialog):
def __init__(self):
QDialog.__init__(self, None)
self.l = QLabel("Foo bar baz", self)
#layout = QVBoxLayout(self)
#layout.addWidget(self.l)
#self.l.move(0, 0)
self.resize(200, 100)
self.move(100, 100)
self.b = b = B()
b.start()
QObject.connect(self, SIGNAL("asignal"), b.bfunc, Qt.QueuedConnection)
QTimer.singleShot(1000, self.afunc)
def afunc (self):
self.emit(SIGNAL("asignal"))
def p(msg):
print int(time() - start), msg
if __name__ == "__main__":
start = time()
p("starting")
app = QApplication(sys.argv)
a = A()
a.show()
#a.afunc()
p("finished in __main__")
sys.exit(app.exec_())
I have discovered, however, that regular Python threads work fine if I use a
data-based/mutex model. I think i'll go with that for now.
On Monday 08 January 2007 13:25, Ole Morten Grodås wrote:
> Hi,
>
> Try using Qt.QueuedConnection. I think that is your problem. Her is an
> example:
>
> import sys
> from time import time
> from PyQt4.QtCore import *
>
> class B (QThread):
> def __init__(self):
> QThread.__init__(self)
> p("Creating thread")
>
> def bfunc(self):
> self.sleep(3)
> p("finished in b()")
>
> def run(self):
> self.exec_()
>
> class A (QObject):
> def __init__(self):
> QObject.__init__(self)
>
> def afunc (self):
> self.emit(SIGNAL("asignal"))
>
>
> def p(msg): print int(time()-start),msg
> if __name__=="__main__":
> start=time()
> p("starting")
> app=QCoreApplication(sys.argv)
> a=A()
> b=B()
> b.start()
> QObject.connect(a,SIGNAL("asignal"),b.bfunc,Qt.QueuedConnection)
> a.afunc()
> p("finished in __main__")
>
> sys.exit(app.exec_())
>
>
> The example gives this output
> 0 starting
> 0 Creating thread
> 0 finished in __main__
> 3 finished in b()
>
>
>
> Regards,
> Ole Morten Grodås
>
> On 1/7/07, Paul Giannaros <ceruleanblaze at gmail.com> wrote:
> > I've been trying to get the main GUI thread to pass a URL to a worker
> > thread
> > and have it download that (without the GUI thread blocking). I haven't
> > been
> > able to do it successfully, i've tried all manner of connects/postEvents.
> > Here is a simplified example:
> >
> > http://rafb.net/p/etOoSH97.html
> >
> > In the example i'm trying to get the main thread to cause the MyThread
> > instance to sleep without blocking the main thread. In MyThread.run I
> > call MyThread.foo (which blocks for 5 seconds) without a problem - the
> > GUI thread
> > isn't being blocked. When I try to get the main thread to indirectly
> > invoke
> > foo (via emitting a signal that the thread listens for, or by posting an
> > event that MyThread picks up in customEvent), then the GUI thread blocks.
> >
> > It's quite crippling. What's the correct way to go about this?
> >
> > Thanks,
> > Paul
> >
> > _______________________________________________
> > PyKDE mailing list PyKDE at mats.imk.fraunhofer.de
> > http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
More information about the PyQt
mailing list