简体   繁体   English

命名管道未在Python中刷新

[英]Named pipe is not flushing in Python

I have a named pipe created via the os.mkfifo() command. 我有一个通过os.mkfifo()命令创建的命名管道。 I have two different Python processes accessing this named pipe, process A is reading, and process B is writing. 我有两个不同的Python进程正在访问此命名管道,进程A正在读取,进程B正在写入。 Process A uses the select function to determine when there is data available in the fifo/pipe. 进程A使用选择功能来确定fifo /管道中是否有可用数据。 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). 尽管事实上在每次写调用之后进程B都会刷新,但是进程A的select函数并不总是返回(就像没有新数据一样,它总是阻塞)。 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. 在广泛研究了这个问题之后,我终于对程序B进行了编程,以在我的实际调用之前和之后添加5KB的垃圾写入,同样,程序A也被编程为忽略那些5KB。 Now everything works fine, and select is always returning appropriately. 现在一切正常,并且select总是正确返回。 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). 我通过注意到进程A的选择将在进程B被杀死时返回(在进行写入和刷新之后,将在读取管道上休眠)来获得这种黑客般的解决方案。 Is there a problem with flush in Python for named pipes? Python中的命名管道冲洗是否存在问题?

What APIs are you using? 您正在使用哪些API? os.read() and os.write() don't buffer anything. os.read()os.write()不缓冲任何内容。

To find out if Python's internal buffering is causing your problems, when running your scripts do "python -u" instead of "python". 要了解Python的内部缓冲是否引起了您的问题,请在运行脚本时执行“ python -u”而不是“ python”。 This will force python in to "unbuffered mode" which will cause all output to be printed instantaneously. 这将强制python进入“非缓冲模式”,这将导致所有输出立即打印。

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. 命名管道的数据严格保留在内存中,直到被读取或关闭FIFO才会释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM