Search code examples
pyqtpyqt4qthread

PyQt Signals And Slots, passing objects between threads


First up, I'm using the old style signals and slots, and QThreads.

I have two threads, one for the GUI, the other a worker. When a signal is emitted from the worker, the GUI handles this in its own thread.

As part of the process the worker is also passing an object to the slot, so I'm using the following:

self.emit(SIGNAL(my_signal(PyQT_PyObject), self.object_to_pass)

The GUI has to interpret this object; in fact it reads the attributes and updates a progress bar accordingly. The slot is defined as:

def my_slot(self, my_state):
    self._state = my_state #so object_to_pass comes in from the worker here

My question is as follows: Is access to the passed object thread safe? What would happen if the worker changed some attribute of the object_to_pass, i.e. would self._state also change?


Solution

  • Access to the passed object is not implicitly thread-safe. Objects are passed through signals by reference, so both threads would be operating on the same object.