0
我正在开发使用命名管道IPC一两种方式,但我这个并发问题:Python和命名管道,如何在重新打开之前等待?
writer.py:
with open("fifo", "w") as f:
f.write("hello")
with open("fifo", "w") as f:
f.write("hello2")
reader.py:
with open("fifo", "r") as f:
f.read()
with open("fifo", "r") as f:
f.read()
问题是:
writer opens the fifo
reader opens the fifo
writer writes "hello"
writer closes the fifo
writer opens the fifo
writer writes "hello2"
reader reads "hellohello2"
writer closes the fifo
reader closes the fifo
reader opens the fifo
reader hangs up
有没有一种方法(不使用协议来控制)s同步并强制作者等待读者在重新开放之前关闭了fifo?
的参数'read'应该是要读取的字符数,不是一个字符串。我甚至不知道这是如何工作的,更不用说“不正确”了。对于这个问题,你真的用“os.mkfifo”创建了一个真正的命名管道,还是只打开一个名为'fifo'的随机文件? – ShadowRanger
抱歉,有关'read()'的复制粘贴失败。其实我读到EOF(-1默认参数读取这个含义)。 我打开一个真正的posix命名管道,由mkfifo(或python脚本中的'os.mkfifo()')制作的 –