您好,登錄后才能下訂單哦!
$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();//子進程結束從主進程傳過來的數據(2)
echo "From Master: $recv\n";
$worker->push(" \n hehe \n ");//這里子進程向主進程發送 hehe(3)
// sleep(2);//注意這里有個sleep
$worker->exit(0);
}
//向子進程通信
foreach($workers as $pid => $process)
{
$process->push("hello worker[$pid]\n");//主進程向子進程發生數據(1)
sleep(3);
$result = $process->pop();//主進程接收從子傳過來的數據(4)
echo "From worker: $result\n";//這里主進程,接受到的子進程的數據
}
for($i = 0; $i < $worker_num; $i++)
{
$ret = swoole_process::wait();//回收結束運行的子進程(5)
$pid = $ret['pid'];
unset($workers[$pid]);
echo "Worker Exit, PID=".$pid.PHP_EOL;
}
運行結果:
例子1:
例子2:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。