Skip to content

Thread-to-thread signals not working #155

@Ruuttu

Description

@Ruuttu

Signals are not getting through if sender and receiver are in different threads.

import time
from PySide.QtCore import *
from PySide.QtGui import *

class NoiseMaker(QObject):
	noise = Signal(int)
	def work(self):
		while True:
			print "Making some noise"
			self.noise.emit( 100 )
			time.sleep(0.5)
			
class Listener(QObject):
	def work(self):
		while True:
			time.sleep(0.5)
			
	def receiveNoise(self, integer):
		print "Got some noise"
		
class App(QApplication):
	def __init__(self):
		super( App, self ).__init__([])
		
		# Create workers
		self.noiseMaker = NoiseMaker()
		self.listener = Listener()
		
		# Create threads
		self.noiseMakerThread = QThread()
		self.listenerThread = QThread()
		
		# Move workers to threads
		self.noiseMaker.moveToThread( self.noiseMakerThread )
		self.listener.moveToThread( self.listenerThread )
		
		# NoiseMaker emits to Listener
		self.noiseMaker.noise.connect( self.listener.receiveNoise )
		
		# Start noise thread
		self.noiseMakerThread.started.connect( self.noiseMaker.work )
		self.noiseMakerThread.start()
		
		# Start listener thread
		self.listenerThread.started.connect( self.listener.work )
		self.listenerThread.start()
		
	
App().exec_()

Expected output:
Making some noise
Got some noise

Actual output:
Making some noise

However:
If you comment out these lines the example works:

		self.noiseMaker.moveToThread( self.noiseMakerThread )
		self.listener.moveToThread( self.listenerThread )

Alternatively, if you move both workers to the same thread, that also works:

		self.noiseMaker.moveToThread( self.noiseMakerThread )
		self.listener.moveToThread( self.noiseMakerThread )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions