作为对之前一篇文章的延伸,这篇文章不幸地死了: select.select issue for sockets and pipes。自从这篇文章以来,我一直在尝试各种各样的事情,但都没有结果,我想看看是否有人知道我错在哪里。我使用select()模块来确定数据何时出现在管道或插座上。插座似乎工作正常,但管道出了问题。在
我将管道设置如下:
pipe_name = 'testpipe'
if not os.path.exists(pipe_name):
os.mkfifo(pipe_name)
管道读数是:
^{pr2}$它作为一个独立的代码段工作得很好,但是当我尝试将它链接到选择。选择功能失效:
inputdata,outputdata,exceptions = select.select([tcpCliSock,xxxx],[],[])
我试过在inputdata参数中输入'pipe'u name'、'testpipe'和'pipein',但是我总是得到一个'not defined'错误。看了其他各种帖子,我想可能是因为管道没有对象标识符,所以我尝试了:
pipein = os.open(pipe_name, 'r')
fo = pipein.fileno()
把“fo”放进选择。选择参数,但得到一个类型错误:需要一个整数。在使用'fo'的这个配置时,我还遇到了一个错误9:错误的文件描述符。任何我做错了什么的想法都会很感激。在
编辑代码: 我已经设法找到了一个方法来解决它,虽然不确定它是否特别整洁-我会对任何评论感兴趣- 修改管道设置:
pipe_name = 'testpipe'
pipein = os.open(pipe_name, os.O_RDONLY)
if not os.path.exists(pipe_name):
os.mkfifo(pipe_name)
管道读数:
def readPipe()
line = os.read(pipein, 1094)
if not line:
return
else:
print line
监视事件的主循环:
inputdata, outputdata,exceptions = select.select([tcpCliSock,pipein],[],[])
if tcpCliSock in inputdata:
readTCP() #function and declarations not shown
if pipein in inputdata:
readPipe()
一切都很好,我现在唯一的问题是在select的任何事件监视开始之前从套接字读取代码。一旦连接到TCP服务器,就会通过套接字发送一个命令,我似乎必须等到管道第一次被读取之后,才能通过这个命令。在
根据docs,select需要
os.open
或类似的文件描述符。所以,您应该使用select.select([pipein], [], [])
作为您的命令。在或者,如果您在linux系统上,您可以使用
epoll
。在相关问题 更多 >
编程相关推荐