[PyQt] Cross-thread signal connections
Andreas Pakulat
apaku at gmx.de
Fri Aug 22 19:59:47 BST 2008
On 22.08.08 14:29:06, Jason Voegele wrote:
> 1) Main (GUI) thread creates a bunch of Task objects.
> 2) Main thread connects signals of Task objects to slots of various
> objects that live in the main thread.
> 3) Main thread submits each task to an Executor.
> 4) Executor creates a few threads. Each thread takes a Task, executes
> it, and repeats until all Tasks have been executed.
> 5) As each Task is executing, it will emit its state change and progress
> signals. Since the Task is executing in a background thread, its
> signals are emitted from that thread.
>
> Now the problem is that the main (GUI) thread is never receiving the
> signals from the Tasks that are executing in the background threads. I
> know that cross-thread signals require an event loop in the receiver,
> and that of course is the case here. I think the problem is that the
> signal/slot connections are made in the main thread in (2), but that
> the signals are not emitted from the same thread in which the
> signal/slot connections were made. It seems like once the Task is
> executing in a separate thread, its existing signal/slot connections
> are lost, or at least do not apply in the new thread. I've tried it
> both with and without calling moveToThread on the Task object, and it
> doesn't seem to make a difference.
>
> Unfortunately, at the time when my application is creating the Task
> objects and connecting to its signals, it has no idea what thread the
> task will end up executing in.
I think you just need to make sure you're passing
QtCore.Qt.QueuedConnection as fith parameter to the connect(). That will
make sure the emitting of the signal goes through the event loop and as
you seem to be emitting signals only in the threads this should be
enough. If you also have slots in your classes that run in a different
thread you'll need to start the event loop in the thread.
Andreas
> [1] This scheme is similar to the facilities offered by the new
> QtConcurrent module, but since that module is not yet available in PyQt
> I cannot use it. I also have other reasons not to use QtConcurrent for
> this particular application, but I won't go into the details here.
AFAIK Phil didn't make that available by purpose. But I can't recall the
details.
Andreas
--
Your fly might be open (but don't check it just now).
More information about the PyQt
mailing list