Python代码在试图打开命名管道进行读取时挂起

Python代码在试图打开命名管道进行读取时挂起,python,file-io,named-pipes,nonblocking,fifo,Python,File Io,Named Pipes,Nonblocking,Fifo,我正在尝试使用命名管道在守护进程和客户端之间建立双向通信。代码在尝试打开用于输入的命名管道时挂起。为什么 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.srvou

我正在尝试使用命名管道在守护进程和客户端之间建立双向通信。代码在尝试打开用于输入的命名管道时挂起。为什么

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 )
从:

当仅使用Ordu或打开FIFO时 O_WRONLY set:

如果设置了O_NONBLOCK,则 仅用于读取的open()应返回 毫不拖延。一个开放的 如果出现以下情况,“仅写入”将返回错误: 当前没有进程打开该文件 用于阅读

如果O_NONBLOCK清除,则为 只读将阻止调用 线程,直到有线程打开文件 为了写作。一个开放的 仅写将阻止调用 线程,直到有线程打开文件 用于阅读


换句话说,当打开命名管道进行读取时,默认情况下,打开的管道将阻塞,直到管道的另一侧打开进行写入。要解决此问题,请使用
os.open()
并在命名管道的读取端传递
os.O\u NONBLOCK

读取并写入管道块,直到连接相应的读卡器或写入器