优雅地关闭ReactPhp应用程序

时间:2015-07-30 10:53:33

标签: php reactphp

这个问题与Consume only N messages from RabbitMQ with react\stomp, ack them separately and then exit非常相关,但更为笼统。

例如,我的I / O操作速度很慢:

$port = 4000;

$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$socket->on('connection', function ($conn) use ($loop){

    $conn->on('data', function ($data) use ($conn, $loop) {

        if ($data == 42) {
            // this instantly stop the loop
            $loop->stop();
        }

        $process = new React\ChildProcess\Process('sleep 5; echo ' . $data);

        $loop->addTimer(0.001, function($timer) use ($process, $conn) {
            $process->start($timer->getLoop());

            $process->stdout->on('data', function($output) use ($conn) {
                if ($output) {
                    $conn->write("> $output");
                }
            });
        });
    });
});
echo "Socket server listening on port $port.\n";
echo "You can connect to it by running: telnet localhost $port\n";
$socket->listen($port);
$loop->run();
echo "exited";

当我运行$loop->run();时,我想在某个时候停止它,例如在接受N个请求或任何其他事件(例如pcntl_signal)或数据断言之后,通过计时器,如示例中所示。

挑战是在退出之前完成所有已启动的工作,我无法弄清楚如何实现它。

在服务器控制台中,我有:

Socket server listening on port 4000.
You can connect to it by running: telnet localhost 4000
exited

在客户端控制台中,我有:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
1
2
3
4
5
> 1
> 2
42
Connection closed by foreign host.

其中以1秒的间隔输入1,2,3,4,5

相反,我想看到这样的事情:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
1
2
3
4
5
> 1
> 2
42
> 3
6
> 4
> 5
Connection closed by foreign host.

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,要正常退出,您需要跟踪正在运行的进程并仅在所有进程完成时停止循环:

$socket->on('connection', function ($conn) use ($loop) {
    $processes = new SplObjectStorage();
    $stop = false;

    $conn->on('data', function ($data) use ($conn, $loop, $processes, &$stop) {
        if ('42' === trim($data)) {
            $stop = true;
            if (!$processes->count()) {
                $loop->stop();
            }
        }

        if ($stop) {
            return;
        }

        $process = new React\ChildProcess\Process('sleep 5; echo ' . $data);
        $processes->attach($process);

        $process->on('exit', function () use ($process, $processes, &$stop, $loop) {
            $processes->detach($process);

            if ($stop && !$processes->count()) {
                $loop->stop();
            }
        });

        $loop->addTimer(0.001, function($timer) use ($process, $conn) {
            $process->start($timer->getLoop());

            $process->stdout->on('data', function($output) use ($conn) {
                if ($output && '42' !== trim($output)) {
                    $conn->write("> $output");
                }
            });
        });
    });
});