小编Aim*_*ard的帖子

Python 中的选择和管道问题

作为上一篇文章的扩展,不幸的是似乎已经死了: select.select issue for sockets and pipe。自从这篇文章以来,我一直在尝试各种方法都无济于事,我想看看是否有人知道我哪里出错了。我正在使用 select() 模块来识别管道或套接字上何时存在数据。套接字似乎工作正常,但管道出现问题。

我已按如下方式设置管道:

pipe_name = 'testpipe'
if not os.path.exists(pipe_name):
    os.mkfifo(pipe_name)
Run Code Online (Sandbox Code Playgroud)

和管道读取是:

pipein = open(pipe_name, 'r')
line = pipein.readline()[:-1]
pipein.close()
Run Code Online (Sandbox Code Playgroud)

它可以完美地作为独立的一段代码运行,但是当我尝试将它链接到 select.select 函数时,它失败了:

inputdata,outputdata,exceptions = select.select([tcpCliSock,xxxx],[],[])
Run Code Online (Sandbox Code Playgroud)

我尝试在 inputdata 参数中输入“pipe_name”、“testpipe”和“pipein”,但我总是收到“未定义”错误。查看其他各种帖子,我认为可能是因为管道没有对象标识符,所以我尝试了:

pipein = os.open(pipe_name, 'r')
fo = pipein.fileno()
Run Code Online (Sandbox Code Playgroud)

并将 'fo' 放在 select.select 参数中,但得到了一个TypeError: an integer is required。我也有一个错误 9:使用此 'fo' 配置时文件描述符错误。任何我做错的想法将不胜感激。

编辑代码:我设法找到了解决它的方法,尽管不确定它是否特别整洁 - 我会对任何评论感兴趣 - 修订管道设置:

pipe_name = 'testpipe'
pipein = os.open(pipe_name, os.O_RDONLY)
if not os.path.exists(pipe_name):
    os.mkfifo(pipe_name)
Run Code Online (Sandbox Code Playgroud)

管道阅读:

def readPipe() …
Run Code Online (Sandbox Code Playgroud)

python sockets select pipe

5
推荐指数
1
解决办法
4070
查看次数

标签 统计

pipe ×1

python ×1

select ×1

sockets ×1