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::push ( string $data ) : int
The data to push to the queue, the default value is 8192 Bytes and the max value is 65536 Bytes.
If failed, it returns FALSE
. Or it returns TRUE
.
In the blocking mode, if the queue is full, the call of this method will block.
In the non-blocking mode, if the queue is full, the call of this method will return FALSE
immediately.
Write and push data into the message queue.
<?php
$workers = [];
$worker_num = 2;
for($i = 0; $i < $worker_num; $i++) {
$process = new OpenSwoole\Process('callback_function', FALSE);
$process->useQueue();
$pid = $process->start();
$workers[$pid] = $process;
}
function callback_function(OpenSwoole\Process $worker) {
echo "Child process started, PID=".$worker->pid."\n";
//recv data from the parent process
$recv = $worker->pop();
echo "Data received from the parent process: $recv\n";
sleep(2);
$worker->exit(0);
}
foreach($workers as $pid => $process) {
$process->push("hello child process [$pid]\n");
}
for($i = 0; $i < $worker_num; $i++)
{
$ret = OpenSwoole\Process::wait();
$pid = $ret['pid'];
unset($workers[$pid]);
echo "Child process exit, PID=".$pid.PHP_EOL;
}