Python和命名管道,如何等待重新打开?

2024-06-25 23:28:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用命名管道开发一个双向IPC,但是我遇到了一个并发问题:

在writer.py公司名称:

with open("fifo", "w") as f:
   f.write("hello")
with open("fifo", "w") as f:
   f.write("hello2")

在阅读器.py公司名称:

^{pr2}$

问题是:

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

是否有一种方法(不使用协议来控制)来同步并强制写入程序在重新打开之前等待读卡器也关闭了fifo?在


Tags: thepy名称helloaswith公司open
1条回答
网友
1楼 · 发布于 2024-06-25 23:28:51

唯一可靠的方法是使用终止字符编写器端,一次读取一个字符,直到终止字符读取器端。在

所以可能是这样的:

在writer.py公司名称:

with open("fifo", "w") as f:
   f.write("hello\n")
with open("fifo", "w") as f:
   f.write("hello2\n")

在阅读器.py在

^{pr2}$

相关问题 更多 >