Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php int Swoole\Process::start ( void )
Create and fork the Swoole Process and return PID of the process, or return FALSE if the fork is failed.
Properties of Swoole 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(Swoole\Process $process) {
sleep(rand(5, 10));
$process->write($process->pid);
echo $process->pid . "\n";
}
for($i=0; $i<$worker_num; $i++) {
$process = new Swoole\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";
});
}