使用Python3.4,在Windows和Unix上,我的意思是使用QSocketNotifier来发现管道已经准备好读取。这是一种人为的例子,因为管道是在一个过程中使用的。无论如何,问题的症结是,当管道上有什么东西可读时,我必须采取行动。
我已经想出了下面的演示程序,但是QSocketNotifier从不发射它的已激活信号。我漏掉了什么明显的东西吗?
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_()
发布于 2014-07-23 10:58:07
实际上,这并不回答如何从可读管道接收通知,但它提供了跨平台的替代方案,即监视套接字(而不是管道):
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()
发布于 2014-07-23 10:43:59
可能与让变量超出范围有关。如果存储所有相关的片段(例如,包装在类中),它就能工作。下面是我的解决方案(使用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_()
https://stackoverflow.com/questions/24917677
复制