• Process->push

    Process->push

    投递数据到消息队列中。

    1. bool Process->push(string $data);
    • $data要投递的数据,长度受限与操作系统内核参数的限制。默认为8192,最大不超过65536
    • 操作失败会返回false,成功返回true
    • 默认模式下(阻塞模式),如果队列已满,push方法会阻塞等待
    • 非阻塞模式下,如果队列已满,push方法会立即返回false
      示例
    1. $workers = [];
    2. $worker_num = 2;
    3. for($i = 0; $i < $worker_num; $i++)
    4. {
    5. $process = new swoole_process('callback_function', false, false);
    6. $process->useQueue();
    7. $pid = $process->start();
    8. $workers[$pid] = $process;
    9. //echo "Master: new worker, PID=".$pid."\n";
    10. }
    11. function callback_function(swoole_process $worker)
    12. {
    13. //echo "Worker: start. PID=".$worker->pid."\n";
    14. //recv data from master
    15. $recv = $worker->pop();
    16. echo "From Master: $recv\n";
    17. sleep(2);
    18. $worker->exit(0);
    19. }
    20. foreach($workers as $pid => $process)
    21. {
    22. $process->push("hello worker[$pid]\n");
    23. }
    24. for($i = 0; $i < $worker_num; $i++)
    25. {
    26. $ret = swoole_process::wait();
    27. $pid = $ret['pid'];
    28. unset($workers[$pid]);
    29. echo "Worker Exit, PID=".$pid.PHP_EOL;
    30. }