PyQt wake main thread from non-QThread
<p>I have a PyQt application that receives information from an external source via callbacks that are called from threads that are not under my control and which are not <code>QThread</code>. What is the correct way to pass such information to the main thread, without polling? Particularly, I want to emit a Qt signal such that I can wake the main thread (or another <code>QThread</code>) upon arrival of new data.</p>

<p>I would handle it the same way I would if you were just polling an external device or library.  Create a separate worker thread that handles the callback, and emits an signal to the main GUI thread.</p>

<pre><code>class Worker(QObject):

    data_ready = pyqtSignal(object, object)

    def callback(self, *args, **kwargs):
        self.data_ready.emit(args, kwargs)


class Window(...)

    def __init__(self):
        ...
        self.worker = Worker(self)
        # Connect worker callback function to your external library
        library.register_callback(self.worker.callback) #??
        ...

        # Mover worker to thread and connect signal
        self.thread = QThread(self)
        self.worker.data_ready.connect(self.handle_data)
        self.worker.moveToThread(self.thread)
        self.thread.start()

    @pyqtSlot(object, object)
    def handle_data(self, args, kwargs):
        # do something with data
</code></pre>

python multithreading qt pyqt qthread