Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Process::wait ([ bool $blocking = true ]): array|bool
if block the execution of this method when called
An array contains the exit code, pid and signal of child process.
<?php
array('code' => 0, 'pid' => 15001, 'signal' => 15);
Wait for the events of child processes.
<?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";