PyQt中的线程和信号问题

时间:2010-12-01 11:07:20

标签: python multithreading pyqt signals

我在PyQt中的Threads之间进行通信时遇到了一些问题。我正在使用信号在两个线程,发送者和监听器之间进行通信。发送方发送消息,预计将由侦听器接收。但是,没有收到任何消息。任何人都可以建议可能出错的地方?我敢肯定它一定很简单,但我一直在寻找几个小时但没找到任何东西。提前谢谢!

from PyQt4 import QtCore,QtGui
import time

class Listener(QtCore.QThread):    
    def __init__(self):
        super(Listener,self).__init__()

    def run(self):
        # just stay alive, waiting for messages
        print 'Listener started'
        while True:
            print '...'
            time.sleep(2)

    def say_hello(self):
        print ' --> Receiver: Hello World!'

class Sender(QtCore.QThread):
    # a signal with no arguments
    signal = QtCore.pyqtSignal()

    def __init__(self):
        super(Sender,self).__init__()
        # create and start a listener
        self.listener = Listener()
        self.listener.start()
        # connect up the signal
        self.signal.connect(self.listener.say_hello)
        # start this thread
        self.start()

    def run(self):
        print 'Sender starting'
        # send five signals
        for i in range(5):
            print 'Sender -->'
            self.signal.emit()
            time.sleep(2)
        # the sender's work is done
        print 'Sender finished'

2 个答案:

答案 0 :(得分:5)

我不确定这是否是你需要的,但它运作正常......

from PyQt4 import QtCore,QtGui
import time

class Listener(QtCore.QThread):
    def __init__(self):
        super(Listener,self).__init__()

    def run(self):
        print('listener: started')
        while True:
            time.sleep(2)

    def connect_slots(self, sender):
        self.connect(sender, QtCore.SIGNAL('testsignal'), self.say_hello)

    def say_hello(self):
        print('listener: received signal')

class Sender(QtCore.QThread):
    def __init__(self):
        super(Sender,self).__init__()

    def run(self):
        for i in range(5):
            print('sender: sending signal')
            self.emit(QtCore.SIGNAL('testsignal'))
            time.sleep(2)
        print('sender: finished')

if __name__ == '__main__':
    o_qapplication = QtGui.QApplication([])
    my_listener = Listener()
    my_sender = Sender()
    my_listener.connect_slots(my_sender)
    my_listener.start()
    my_sender.start()
    i_out = o_qapplication.exec_()

答案 1 :(得分:4)

问题是QThread要发送/接收SIGNALS,它需要运行EventLoop。你不是,所以线程没有机会回应。看看这篇博文:You're doing it wrong

这是一个适合我的例子 - 注意你需要在连接信号之前调用moveToThread(这在博客中没有提到 - 不确定它是否特定于PyQt),否则它们将在主要运行线程。

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import time

class MyThread(QThread):
    def __init__(self, name):
        super(MyThread, self).__init__()
        self.setObjectName(name)

    def run(self):
        print "RUN", QThread.currentThread().objectName(), QApplication.instance().thread().objectName()
        self.exec_()
        print "RUN DONE", QThread.currentThread().objectName()

class Producer(QObject):
    def __init__(self, parent=None):
        super(Producer, self).__init__(parent)

    def Start(self):
        for i in range(5):
            print "Producer",i,QThread.currentThread().objectName()
            self.emit(SIGNAL("testsignal"),i)
            time.sleep(2)
        time.sleep(1)
        qApp.quit()

class Consumer(QObject):
    def __init__(self, parent=None):
        super(Consumer, self).__init__(parent)

    def Consume(self, i):
        print "Consumed",i,QThread.currentThread().objectName()

if __name__ == "__main__":
    app = QApplication([])
    producer = Producer()
    consumer = Consumer()
    QThread.currentThread().setObjectName("MAIN")
    producerThread = MyThread("producer")
    consumerThread = MyThread("consumer")
    producer.moveToThread(producerThread)
    consumer.moveToThread(consumerThread)
    producerThread.started.connect(producer.Start)
    producer.connect(producer, SIGNAL("testsignal"), consumer.Consume)
    def aboutToQuit():
        producerThread.quit()
        consumerThread.quit()
        time.sleep(1)
    qApp.aboutToQuit.connect(aboutToQuit)
    consumerThread.start()
    time.sleep(.1)
    producerThread.start()
    sys.exit(app.exec_())