How to signal slots in a GUI from a different process?

前端 未结 5 2026
囚心锁ツ
囚心锁ツ 2021-01-31 19:52

Context: In Python a main thread spawns a 2nd process (using multiprocessing module) and then launches a GUI (using PyQt4). At this point the main thread blocks until the GUI is

5条回答
  •  执笔经年
    2021-01-31 20:31

    A quite interesting topic. I guess having a signal that works between threads is a very useful thing. How about creating a custom signal based on sockets? I haven't tested this yet, but this is what I gathered up with some quick investigation:

    class CrossThreadSignal(QObject):
        signal = pyqtSignal(object)
        def __init__(self, parent=None):
            super(QObject, self).__init__(parent)
            self.msgq = deque()
            self.read_sck, self.write_sck = socket.socketpair()
            self.notifier = QSocketNotifier(
                               self.read_sck.fileno(), 
                               QtCore.QSocketNotifier.Read
                            )
            self.notifier.activated.connect(self.recv)
    
        def recv(self):
            self.read_sck.recv(1)
            self.signal.emit(self.msgq.popleft())
    
        def input(self, message):
            self.msgq.append(message)
            self.write_sck.send('s')
    

    Might just put you on the right track.

提交回复
热议问题