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

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\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 OpenSwoole\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;

OpenSwoole\Process::signal(SIGCHLD, function($sig) use (&$i){
        // there may be multi processes which exit at the same time
        while($ret = OpenSwoole\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 September 1, 2022