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 int OpenSwoole\Process::start ( void )
Create and fork the OpenSwoole Process and return PID of the process, or return FALSE if the fork is failed.
Properties of OpenSwoole Process:
<?php
$process->pid // the pid of child process
$process->pipe // the file descriptor of pipe
The pid of child process, or return FALSE if the fork is failed.
You can use swoole_errno
and swoole_strerror
to get the code and extra information of error.
<?php
$workers = [];
$worker_num = 3;
function process(OpenSwoole\Process $process) {
sleep(rand(5, 10));
$process->write($process->pid);
echo $process->pid . "\n";
}
for($i=0; $i<$worker_num; $i++) {
$process = new OpenSwoole\Process('process');
$pid = $process->start();
$workers[$pid] = $process;
echo "create a child process: $pid\n";
}
foreach($workers as $process) {
swoole_event_add($process->pipe, function ($pipe) use($process){
$data = $process->read();
echo "RECV: " . $data . "\n";
});
}