Python 代码在尝试打开命名管道进行读取时挂起
我正在尝试使用命名管道在守护程序和客户端之间设置两种方式的通信。尝试打开用于输入的命名管道时代码挂起,为什么?
class comm(threading.Thread):
def __init__(self):
self.srvoutf = './tmp/serverout'
self.srvinf = './tmp/serverin'
if os.path.exists(self.srvoutf):
self.pipein = open(self.srvoutf, 'r')
#-----------------------------------------------------Hangs here
else:
os.mkfifo(self.srvoutf)
self.pipein = open(self.srvoutf, 'r')
#-----------------------------------------------------or here
if os.path.exists(self.srvinf):
self.pipeout = os.open(self.srvinf, os.O_WRONLY)
else:
os.mkfifo(self.srvinf)
self.pipeout = os.open(self.srvinf, os.O_WRONLY)
threading.Thread.__init__ ( self )
I am trying to setup two way communication between a daemon and a client using named pipes. The code hangs while trying to open the named pipe used for input Why?
class comm(threading.Thread):
def __init__(self):
self.srvoutf = './tmp/serverout'
self.srvinf = './tmp/serverin'
if os.path.exists(self.srvoutf):
self.pipein = open(self.srvoutf, 'r')
#-----------------------------------------------------Hangs here
else:
os.mkfifo(self.srvoutf)
self.pipein = open(self.srvoutf, 'r')
#-----------------------------------------------------or here
if os.path.exists(self.srvinf):
self.pipeout = os.open(self.srvinf, os.O_WRONLY)
else:
os.mkfifo(self.srvinf)
self.pipeout = os.open(self.srvinf, os.O_WRONLY)
threading.Thread.__init__ ( self )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 open() 规范:
换句话说,当您打开命名管道进行读取时,默认情况下,该打开将阻塞,直到管道的另一端打开进行写入。要解决此问题,请使用 os.open() 并在命名管道的读取端传递 os.O_NONBLOCK 。
From the specification for open():
In other words, when you open a named pipe for reading, by default the open will block until the other side of the pipe is opened for writing. To fix this, use
os.open()
and passos.O_NONBLOCK
on the read side of the named pipe.