Windows 上的 PHP 命名管道
好的,这是场景。假设我有一个 php 脚本,该脚本应该等待某些信息可用,并且我认为如果我可以使该脚本块而不是忙循环直到该信息可用,那么这将是一个很好的解决方案。信息本身将由 php 脚本的另一个实例提供。假设我希望阻止 X 个此类请求,直到这 1 个脚本实例提供此信息。 我想我可以使用命名管道,读取器进程将阻塞等待写入管道,而写入器将通过管道或其他方式提供所需的信息来写入并立即解锁所有读取器。 我找到了 posix_mkfifo 但它显然不适用于 Windows,是否有其他方法可以在 Windows 和 *nix 上使用。 或者也许有某种方法可以阻止一堆请求,并让另一个请求在时机成熟时“释放它们”。 我真的不想诉诸繁忙的循环,我想在不使用 1 秒延迟和 sutch 的情况下节省 cpu。
Ok here's the scenario. Let's say i got a php script that is supposed to wait for certain information to become available , and i figured it'd be a nice solution if i could make this script block instead of busy looping until that information becomes available. The information itself would be provided by another instance of a php script. And lets say i want X number of such requests to block until this 1 instance of a script delivers this information.
I figured i could use a named pipe , the reader processes would block waiting for the pipe to be written to, and the writer would write and instantly unblock all the readers by giving the needed information through the pipe or otherwise.
i found posix_mkfifo but it does not work on windows aparently, is there any other way to do this that would work on both windows and *nix.
Or maybe there is some sort of way to just make a bunch of requests block , and for another request to "set them free" once it's time.
I really don't want to resort to busy loops, i want to spare the cpu without using 1 second delays and sutch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有可用的套接字扩展(或
stream_socket_server()
),您可以在脚本中创建一个套接字服务器,为其他脚本提供信息 - 让我们将该脚本称为“主脚本”。然后,您可以让您想要阻止的所有脚本(“从属”)对本地主机执行fsockopen()
,然后将它们设置为fgets()
、fwrite()
等将阻塞,直到数据通过这些套接字可用为止。这并不是最有效的方法,因为您必须将 IPC 分出到 TCP 堆栈,但如果您希望它在 Windoze 上工作,则不能使用 *nix 域套接字,所以这就是您所坚持的。
我能想到的唯一一件你可以做的事情是涉及临时文件和flock(),因为默认情况下它会阻塞,直到它获得锁为止。这可能是最可移植的选项,因为自远古以来,
flock()
的行为在任何地方都是相同的,但它也可能无法(并且可能不会)在作为 ISAPI 模块运行的 PHP 实例上工作(请参阅 PHP 手册)。If you have the sockets extension available (or
stream_socket_server()
) you could create a socket server in the script that provides the information to the other scripts - lets call that script the 'master'. You could then have your all the scripts that you want to block - the 'slaves' - do anfsockopen()
to localhost and then set them up so thatfgets()
,fwrite()
etc will block until the data is made available over those sockets.This is not exactly the most efficient approach as you have to sub the IPC out to the TCP stack, but if you want it to work on Windoze you can't use *nix domain sockets, so that's what you stuck with.
The only other thing I can think of that you could do is something involving a temp file and
flock()
, since by default it will block until it can obtain a lock. This is probably the most portable option, since the behaviour offlock()
has been the same everywhere since time immemorial, but it also may not (and probably wont) work on PHP instances running as an ISAPI module (see the PHP manual).