简体   繁体   English

在PHP中非阻塞打开管道

[英]Non-blocking open of pipe in PHP

I have to open a named fifo from a php script that needs to write to the fifo, but it may not always be opened on the read end. 我必须从需要写入到fifo的php脚本中打开一个名为fifo的文件,但它不一定总是在读取端打开。 Now I'm using fopen , but it always blocks when the read end of the fifo it's not opened. 现在我正在使用fopen ,但是当未打开fifo的读取端时,它始终会阻塞。 Is there an equivalent of the UNIX's int open(pname, O_WRONLY | O_NONBLOCK) in php so that when the fifo it's not opened to return imediately? 在php中是否有等效于UNIX的int open(pname, O_WRONLY | O_NONBLOCK) ,以便在未打开fifo时立即返回?

No direct equivalent to the C function you describe. 没有直接等价于您描述的C函数。 But you could use: 但是您可以使用:

fopen($pipename, "w+")

The operation will return immediately, because it obtains read/write access to the pipe, so a read handle will always be opened (Opening plain write access the pipe will block for a reader to open the other end) 该操作将立即返回,因为它获得了对管道的读/写访问权限,因此将始终打开读取句柄(打开普通写访问权限时,该管道将阻塞供读取器打开另一端)

Note: Usually people do not want to do this. 注意:通常人们不希望这样做。 There is no way to know if the real pipe reader actually got the message or not. 无法知道真正的管道读取器是否真正收到了该消息。 This might result to data loss. 这可能会导致数据丢失。

Named pipes are best suited in applications where it can be guaranteed that both writer and reader run in parallel. 命名管道最适合可以保证写入器和读取器并行运行的应用程序。

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

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