我有以下示例代码:
#! /usr/bin/python2.7
import os
import errno
FIFO = 'mypipe'
try:
os.mkfifo(FIFO)
except OSError as oe:
if oe.errno != errno.EEXIST:
raise
with open(FIFO) as fifo:
test=fifo.read()
print("FIFO opened")
while True:
print "reading fifo"
data = fifo.read()
print "python read"
if len(data) == 0:
print("Writer closed")
break
print "about to open pipe for writing"
otherpipe = open('mypipereader', 'r+')
otherpipe.write('hello back!')
这很好用。事实上,当我尝试打开管道以在另一个程序中写入时,当我尝试打开管道以在另一个程序中写入时,它完全按照我想要的方式执行,但出于某种原因在另一个脚本中,如....
THEPIPE = open('mypipe', 'w')
THEPIPE.write("你好!")
它继续挂起!曾经有人告诉我,这与内核无法在读取之前打开写入管道有关...有什么办法可以解决这个问题吗?
提前致谢!
最佳答案
我一直在努力解决这个问题,最终通过首先创建一个文件描述符解决了我的问题:
import os
fifo = 'my-fifo'
os.mkfifo(fifo)
fd = os.open(fifo, os.O_RDWR) #non-blocking
f = os.fdopen(fd, 'w') #also non-blocking
关于python - Opening a Pipe for Writing in Python 挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43834991/