Swoole\Process::wait ([ bool $blocking = true ])

Latest version: pecl install openswoole-4.11.1

Declaration

<?php Swoole\Process::wait ([ bool $blocking = true ]): array|bool

Parameters

blocking

if block the execution of this method when called

Return

array|bool

An array contains the exit code, pid and signal of child process.

<?php
array('code' => 0, 'pid' => 15001, 'signal' => 15);

Description

Wait for the events of child processes.

Example

<?php
$child_num = 3;
$child_processes = [];
for($i = 1; $i <= $child_num; $i++)
{
    $process = new Swoole\Process(function($worker){
        echo "the pid of child process is " . $worker->pid . "\n";

        $worker->name("php child process");

        sleep(rand(2, 6));

        $worker->exit(rand(1, 255));

    }, FALSE);

    $res = $process->useQueue(0, 2);

    $pid = $process->start();

    $child_processes[(string)$pid] = $process;
}

$i = 1;

Swoole\Process::signal(SIGCHLD, function($sig) use (&$i){
        // there may be multi processes which exit at the same time
        while($ret = Swoole\Process::wait(0)){
            var_dump($ret);
        }

        var_dump($i);

        if($i >= 3)
        {
            exit(0);
        }

        $i++;
    }
);

echo "Wait for exit of child process\n";
Last updated on March 31, 2022