Process->push
投递数据到消息队列中。
bool Process->push(string $data);
- $data要投递的数据,长度受限与操作系统内核参数的限制。默认为8192,最大不超过65536
- 操作失败会返回false,成功返回true
- 默认模式下(阻塞模式),如果队列已满,
push
方法会阻塞等待 - 非阻塞模式下,如果队列已满,
push
方法会立即返回false
示例
$workers = [];
$worker_num = 2;
for($i = 0; $i < $worker_num; $i++)
{
$process = new swoole_process('callback_function', false, false);
$process->useQueue();
$pid = $process->start();
$workers[$pid] = $process;
//echo "Master: new worker, PID=".$pid."\n";
}
function callback_function(swoole_process $worker)
{
//echo "Worker: start. PID=".$worker->pid."\n";
//recv data from master
$recv = $worker->pop();
echo "From Master: $recv\n";
sleep(2);
$worker->exit(0);
}
foreach($workers as $pid => $process)
{
$process->push("hello worker[$pid]\n");
}
for($i = 0; $i < $worker_num; $i++)
{
$ret = swoole_process::wait();
$pid = $ret['pid'];
unset($workers[$pid]);
echo "Worker Exit, PID=".$pid.PHP_EOL;
}