Python 中的命名管道未刷新
我有一个通过 os.mkfifo() 命令创建的命名管道。我有两个不同的Python进程访问这个命名管道,进程A正在读取,进程B正在写入。进程 A 使用 select 函数来确定 fifo/管道中何时有可用数据。尽管进程 B 在每次写入调用后都会刷新,但进程 A 的 select 函数并不总是返回(它一直阻塞,就好像没有新数据一样)。在广泛研究这个问题之后,我最终只是对进程 B 进行了编程,以在实际调用之前和之后添加 5KB 的垃圾写入,同样,对进程 A 进行编程以忽略这些 5KB。现在一切正常,并且 select 总是正确返回。我注意到如果进程 B 被终止,进程 A 的 select 将会返回(在写入和刷新之后,它将在读管道上休眠),所以我想到了这个 hack-ish 解决方案。 Python 中命名管道的刷新有问题吗?
I have a named pipe created via the os.mkfifo() command. I have two different Python processes accessing this named pipe, process A is reading, and process B is writing. Process A uses the select function to determine when there is data available in the fifo/pipe. Despite the fact that process B flushes after each write call, process A's select function does not always return (it keeps blocking as if there is no new data). After looking into this issue extensively, I finally just programmed process B to add 5KB of garbage writes before and after my real call, and likewise process A is programmed to ignore those 5KB. Now everything works fine, and select is always returning appropriately. I came to this hack-ish solution by noticing that process A's select would return if process B were to be killed (after it was writing and flushing, it would sleep on a read pipe). Is there a problem with flush in Python for named pipes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用什么 API?
os.read()
和os.write()
不缓冲任何内容。What APIs are you using?
os.read()
andos.write()
don't buffer anything.要查明 Python 的内部缓冲是否导致了您的问题,请在运行脚本时执行“python -u”而不是“python”。这将迫使 python 进入“无缓冲模式”,这将导致所有输出立即打印。
To find out if Python's internal buffering is causing your problems, when running your scripts do "python -u" instead of "python". This will force python in to "unbuffered mode" which will cause all output to be printed instantaneously.
刷新操作与命名管道无关;命名管道的数据严格保存在内存中,在读取或关闭 FIFO 之前不会释放。
The flush operation is irrelevant for named pipes; the data for named pipes is held strictly in memory, and won't be released until it is read or the FIFO is closed.