如何通过QSocketNotifier发出管道已准备好读取的信号?

时间:2014-07-23 18:02:44

标签: python windows qt unix pyqt

在Windows和Unix上使用Python 3.4,我的意思是使用QSocketNotifier来发现管道已准备就绪。这是一种人为的例子,因为管道在一个过程中使用。无论哪种方式,问题的关键在于,当管道上有东西要读时,我需要采取行动。

我已经提出了以下演示程序,但QSocketNotifier从不发出activated信号。我错过了一些明显的东西吗?

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import os


def can_read(fd):
   print('File {} can be read'.format(fd))


def start():
   print('Starting')
   reader, writer = os.pipe()
   notifier = QSocketNotifier(reader, QSocketNotifier.Read, w)
   notifier.setEnabled(True)
   notifier.activated.connect(can_read)
   os.write(writer, b'a')
   os.close(writer)


app = QApplication([])
QTimer.singleShot(0, start)
w = QMainWindow()
w.show()
app.exec_()

2 个答案:

答案 0 :(得分:1)

可能与让变量超出范围有关。如果你存储所有相关的部分,例如包装在一个类中,它就可以了。这是我的解决方案(使用pyqt4,我的道歉),它一直运行直到窗口关闭:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import os

class win(QMainWindow):


    def can_read(self,fd):
       print('File {} can be read'.format(fd))


    def start(self):
       print('Starting')
       self.reader, self.writer = os.pipe()
       notifier = QSocketNotifier(self.reader, QSocketNotifier.Read, self)
       notifier.setEnabled(True)
       notifier.activated.connect(self.can_read)
       os.write(self.writer, b'a')
       os.close(self.writer)


app = QApplication([])
#QTimer.singleShot(0, start)
w = win()
w.start()
w.show()
app.exec_()

答案 1 :(得分:1)

事实上,这并没有回答如何从可读管道接收通知,但它确实提供了跨平台替代方案,即监控套接字(而不是管道):

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import socket

def _create_sock_pair(port=0):
    """Create socket pair.

    If socket.socketpair isn't available, we emulate it.
    """
    # See if socketpair() is available.
    have_socketpair = hasattr(socket, 'socketpair')
    if have_socketpair:
        client_sock, srv_sock = socket.socketpair()
        return client_sock, srv_sock

    # Create a non-blocking temporary server socket
    temp_srv_sock = socket.socket()
    temp_srv_sock.setblocking(False)
    temp_srv_sock.bind(('', port))
    port = temp_srv_sock.getsockname()[1]
    temp_srv_sock.listen(1)

    # Create non-blocking client socket
    client_sock = socket.socket()
    client_sock.setblocking(False)
    try:
        client_sock.connect(('localhost', port))
    except socket.error as err:
        # Error 10035 (operation would block) is not an error, as we're doing this with a
        # non-blocking socket.
        if err.errno != 10035:
            raise

    # Use select to wait for connect() to succeed.
    import select
    timeout = 1
    readable = select.select([temp_srv_sock], [], [], timeout)[0]
    if temp_srv_sock not in readable:
        raise Exception('Client socket not connected in {} second(s)'.format(timeout))
    srv_sock, _ = temp_srv_sock.accept()

    return client_sock, srv_sock


def can_read():
    print('Server can read')
    app.quit()


def write():
    print('Client writing')
    client_sock.send(b'a')
    client_sock.close()


app = QApplication([])

client_sock, srv_sock = _create_sock_pair()
notifier = QSocketNotifier(srv_sock.fileno(), QSocketNotifier.Read)
notifier.activated.connect(can_read)
write()

w = QMainWindow()
w.show()
app.exec_()
srv_sock.close()