Python 中的命名管道未刷新

发布于 2024-08-19 20:23:54 字数 373 浏览 13 评论 0原文

我有一个通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单身狗的梦 2024-08-26 20:23:54

您使用什么 API? os.read()os.write() 不缓冲任何内容。

What APIs are you using? os.read() and os.write() don't buffer anything.

雪落纷纷 2024-08-26 20:23:54

要查明 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.

月亮是我掰弯的 2024-08-26 20:23:54

刷新操作与命名管道无关;命名管道的数据严格保存在内存中,在读取或关闭 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文