swoole_process->useQueue

������Ϣ������Ϊ���̼�ͨ�š�useQueue��������2����ѡ������

bool swoole_process->useQueue (int $msgkey = 0, int $mode = 1);

����

<?php
$workers = [];
$worker_num = 2;
for($i = $worker_num; $i > 0; $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
    while(true)
    {
        $recv = $worker->pop();
        echo "From Master: $recv\n";
    }
    sleep(2);
    $worker->exit(0);
}
while(true)
{
    /**
     * @var $process swoole_process
     */
    $pid = array_rand($workers);
    $process = $workers[$pid];
    $process->push("hello worker[$pid]\n");
    sleep(1);
}
for($i = $worker_num; $i > 0; $i--)
{
    $ret = swoole_process::wait();
    $pid = $ret['pid'];
    unset($workers[$pid]);
    echo "Worker Exit, PID=".$pid.PHP_EOL;
}
  • ʹ��ģʽ2�󣬴������ӽ����޷����е���ͨ�ţ����緢���ض��ӽ��̡�
  • $process����δִ��start��Ҳ����ִ��push/pop���������/��ȡ����
  • ��Ϣ����ͨ�ŷ�ʽ��ܵ����ɹ��á���Ϣ���в�֧��EventLoop��ʹ����Ϣ���к�ֻ��ʹ��ͬ������ģʽ

������

��1.9.2����߰汾��������Process::IPC_NOWAIT��֧�֣��ɽ���������Ϊ���������ڷ�����ģʽ�£�������������push�����������ѿյ���pop����ʱ�����������������ء�

//����Ϊ������ģʽ
$process->useQueue($key, $mode | Process::IPC_NOWAIT);