开发者

PHP Named pipes on windows

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 provide开发者_如何转开发d 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.


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 an fsockopen() to localhost and then set them up so that fgets(),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 of flock() 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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜