[PyKDE] Correct cross-thread communication example?

Ole Morten Grodås grodaas at gmail.com
Mon Jan 8 21:45:26 GMT 2007


Hi,
That is a bit odd. Are you using the latest version of SIP and PyQt?.  When
I'm running the example I posted to you without the
Qt.QueuedConnectionparameter i get the following output:

0 starting
0 Creating thread
3 finished in b()
3 finished in __main__

The worker thread is locking the GUI thread. When I use the
Qt.QueuedConnection parameter in the connect call i get the following ouput:

0 starting
0 Creating thread
0 finished in __main__
3 finished in b()

As you can see the worker thread is not blocking the GUI thread.

Regards,
Ole



On 1/8/07, Paul Giannaros <ceruleanblaze at gmail.com> wrote:
>
> 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
>
> _______________________________________________
> PyKDE mailing list    PyKDE at mats.imk.fraunhofer.de
> http://mats.imk.fraunhofer.de/mailman/listinfo/pykde
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20070108/419e767d/attachment.html


More information about the PyQt mailing list