如何在PHP中的不同函数甚至不同进程之间使用命名管道而无需fork? [英] How to use named pipes in PHP between different functions or even different processes without fork?

查看:76
本文介绍了如何在PHP中的不同函数甚至不同进程之间使用命名管道而无需fork?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个Ajax Web应用程序,一个特定的游戏.两个Web客户端必须通过PHP服务器相互通信.解决此问题的方法是在客户端与服务器之间以及服务器与客户端之间使用Ajax.每个客户端都使用Ajax创建一个单独的服务器进程.我希望这两个服务器进程通过MySQL和命名管道进行通信.我需要命名管道以获得整个应用程序的立即响应.

I want to write an Ajax web application, a game to be specific. Two web clients have to communicate with each other via the PHP server. My approach to solve this is to use Ajax between client and server and server and client. Each client creates a separate server process using Ajax. I want that these two server processes communicate via MySQL and via named pipes. I need the named pipes to get immediate response for the whole application.

我不能使用一个服务器进程,该服务器进程首先创建一个管道,然后分叉到两个使用该管道的进程中.当Web浏览器发送请求时,Web应用程序将创建服务器进程.因此,我需要命名管道,其中每个进程只知道命名管道的文件名.他们无法交换文件句柄(至少我不知道如何).

I cannot use one server process, which first creates a pipe and then forks into two processes, which use the pipe. Web applications create server processes when the web-browser sends a request. So, I need named pipes, where each process doesn't know more than the file name of the named pipe. They cannot exchange file handles (at least I don't know how).

我的问题是,只要使用PHP,命名管道确实可以正常工作 在同一功能中使用:

My problem is that named pipes in the PHP way indeed work as long as they are used within the same function:

public function writeAndReadPipe_test(){
    $pipeA = fopen("testpipe",'r+');
    fwrite($pipeA, 'ABCD');

    $pipeB = fopen("testpipe",'r+');
    $content = fread($pipeB, 4);
    echo "[" . $content . "]<br>\n";
}

public function testingPipes_same_function(){
    posix_mkfifo("testpipe", 0777);
    $this->writeAndReadPipe_test();
}

但是,当我使用不同的功能时, fread($pipeB, 4) 命令将阻止整个应用程序:

But, when I use different functions, then the fread($pipeB, 4) command blocks the whole application:

public function writePipe_test(){
    $pipeA = fopen("testpipe",'r+');
    fwrite($pipeA, 'ABCD');
}

public function readPipe_test(){
    $pipeB = fopen("testpipe",'r+');
    $content = fread($pipeB, 4);
    echo "[" . $content . "]<br>\n";
}

public function testingPipes_different_functions(){
    posix_mkfifo("testpipe", 0777);
    $this->writePipe_test();
    $this->readPipe_test();
}

有人知道为什么吗?第一步,我该怎么做才能使其在不同的功能之间工作?在第二步中,它甚至应该在不同的进程之间工作!我发现当作者在读者读取之前关闭管道时,我也会遇到问题 它.我想该函数在结束时会自动关闭,但这只是一个猜测.

Does anybody know why? And what can I do to make it work between different functions in the first step? In the second step, it should work even between different processes! I found out that I also get a problem when the writer closes the pipe before the reader reads from it. I suppose that the function closes it automatically when it ends, but this is only a guess.

如果PHP方式行不通,我打算让PHP打开命令行,生成BASH命令并执行它们.只要我的Web服务器在LAMP环境中工作,这在任何情况下都应该有效.缺点是它不能在WAMP环境中使用.

If the PHP way does not work, I plan to let PHP open a command line, generate BASH commands and let execute them. This should work in any case as long as my web-server works in a LAMP environment. Disadvantage is that it will not work in WAMP environments.

那么,有人对此有想法吗?

So, has anybody some ideas to this?

P.S: 我需要阻塞管道,以使阅读器等待事件发送.我知道管道可以使用

P.S: I need blocking pipes to let the reader wait until an event is sent. I know that the pipes can work in a non-blocking mode using

stream_set_blocking($pipe,false);

等等,但是整个想法是不使用管道进行轮询就可以做到这一点,这会唤醒 事件触发后,读者立即起来.

or so, but the whole idea is to do it without polling just using a pipe, which wakes the reader up as soon as an event is fired.

推荐答案

在像这样的单独函数中使用管道时,写入管道A似乎又被关闭/丢弃($ pipeA的本地作用域).假设必须打开管道以进行读取和/或写入,以保留任何信息,这确实是有道理的.虽然我不知道内在的魔力.

When you use the pipes in separate functions like this, the write pipe A would seem to be closed/discarded again (local scope of $pipeA). The assumption would be that the pipe must be opened for reading and/or writing in order to retain any info, which makes sense really. Though I don't know the inner magic.

您还可以观察到,当您从另一个进程(如echo magic >> testpipe)馈送管道时,阻塞的读取调用成功.因此,您已经完成了第2步,但是您需要一些管道句柄管理.

You can also observe that your blocking read-call succeeds when you feed the pipe from another process (like echo magic >> testpipe). So you'd already have step 2 done, but you need some pipehandle management.

如果您按以下方式进行更改,则可以使用:

If you change it as follows it'd work:

    private $pipeA;
    public function writePipe_test(){
        $this->pipeA = fopen("testpipe",'r+');
        fwrite($this->pipeA, 'ABCD');
    }

为此,或将$ pipeA设置为具有全局范围..

or setting $pipeA to have global scope, for that matter..

这篇关于如何在PHP中的不同函数甚至不同进程之间使用命名管道而无需fork?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆