濮阳杆衣贸易有限公司

主頁 > 知識庫 > PHP下用Swoole實現(xiàn)Actor并發(fā)模型的方法

PHP下用Swoole實現(xiàn)Actor并發(fā)模型的方法

熱門標簽:電銷機器人問門薩維品牌my 400電話蘭州申請請 外呼系統(tǒng)能給企業(yè)帶來哪些好處 開發(fā)地圖標注類網站 百度地圖標注偏差 百度地圖怎樣標注圖標 咸寧銷售電銷機器人系統(tǒng) 廣東廣州在怎么申請400電話 余姚電話機器人

什么是Actor?

Actor對于PHPer來說,可能會比較陌生,寫過Java的同學會比較熟悉,Java一直都有線程的概念(雖然PHP有Pthread,但不普及),它是一種非共享內存的并發(fā)模型,每個Actor內的數(shù)據(jù)獨立存在,Actor之間通過消息傳遞的形式進行交互調度,且Actor是一種高度抽象化的編程模型,非常適合于游戲、硬件行業(yè)。

Swoole協(xié)程與信箱

得益于Swoole4.x,我們可以基于Swoole的協(xié)程與Channel快速實現(xiàn)一個信箱模式調度。模擬代碼如下:

use Swoole\Coroutine\Channel;
go(function (){
  //創(chuàng)建十個信箱通道
  $mailBoxes = [];
  for ($i = 1;$i = 10;$i++){
    $mailBoxes[$i] = new Channel(16);
  }
  //模擬master 郵局調度,隨機像一個信箱投遞消息
  go(function ()use($mailBoxes){
    while (1){
      \co::sleep(2);
      $key = rand(1,10);
      ($mailBoxes[$key])->push(time());
    }
  });
  //模擬actor 實體消費
  for ($i = 1;$i = 10;$i++){
    go(function ()use($mailBoxes,$i){
      while (1){
        $msg = ($mailBoxes[$i])->pop();
        echo "Actor {$i} recv msg : {$msg} \n";
      }
    });
  }
});

以上代碼執(zhí)行輸出:

php test.php
Actor 8 recv msg : 1559622691
Actor 10 recv msg : 1559622693
Actor 1 recv msg : 1559622695
Actor 5 recv msg : 1559622697

協(xié)程通道每次在POP遇到無數(shù)據(jù)的時候,都會自動讓出執(zhí)行權(具體可以去看Swoole協(xié)程調度)

Actor庫

基于上面的原理,我們實行了一個多進程分布的協(xié)程Actor庫

composer require easyswoole/actor=2.x-dev

我們依賴dev庫進行測試,生產可以自己依賴stable版本

進程關系

Easyswoole的Actor模型中,存在兩組進程,一組是proxy進程,用來實現(xiàn)Actor對外服務,一組是worker進程,proxy進程與worker進程之間通過unixsock進行通訊,而Actor實例就均勻的分布worker之中。

樣例代碼

比如在一個聊天室中,我們可以定義一個房間模型。

namespace EasySwoole\Actor\Test;


use EasySwoole\Actor\AbstractActor;
use EasySwoole\Actor\ActorConfig;

class RoomActor extends AbstractActor
{
  public static function configure(ActorConfig $actorConfig)
  {
    $actorConfig->setActorName('Room');
  }
  public function onStart()
  {
    //每當一個RoomActor實體被創(chuàng)建的時候,都會執(zhí)行該回調
    var_dump('room actor '.$this->actorId().' start');
  }
  public function onMessage($msg)
  {
    //每當一個RoomActor實體收到外部消息的時候,都會執(zhí)行該回調當
    var_dump('room actor '.$this->actorId().' onmessage: '.$msg);
    return 'reply at '.time();
  }
  public function onExit($arg)
  { 
    //每當一個RoomActor實體退出的時候,都會執(zhí)行該回調
    var_dump('room actor '.$this->actorId().' exit at arg: '.$arg);
    return 'exit at '.time();
  }
  protected function onException(\Throwable $throwable)
  {
    //每當一個RoomActor出現(xiàn)異常的時候,都會執(zhí)行該回調
    var_dump($throwable->getMessage());
  }
}

在cli模式下創(chuàng)建一個Actor服務

use EasySwoole\Actor\Actor;
use EasySwoole\Actor\Test\RoomActor;
use EasySwoole\Actor\ProxyProcess;

Actor::getInstance()->register(RoomActor::class);
$list = Actor::getInstance()->generateProcess();

foreach ($list['proxy'] as $proxy){
  /** @var ProxyProcess $proxy */
  $proxy->getProcess()->start();
}
foreach ($list['worker'] as $actors){
  foreach ($actors as $actorProcess){
    /** @var ProxyProcess $actorProcess */
    $actorProcess->getProcess()->start();
  }
}
while($ret = \Swoole\Process::wait()) {
  echo "PID={$ret['pid']}\n";
}

創(chuàng)建一個cli測試腳本

use EasySwoole\Actor\Actor;
use EasySwoole\Actor\Test\RoomActor;
Actor::getInstance()->register(RoomActor::class);

go(function (){
  $actorId = RoomActor::client()->create('create arg1');
  var_dump($actorId);
  \co::sleep(3);
  var_dump(RoomActor::client()->send($actorId,'this is msg'));
  \co::sleep(3);
  var_dump(RoomActor::client()->exit($actorId,'this is exit arg'));
  \co::sleep(3);
  RoomActor::client()->create('create arg2');
  \co::sleep(3);
  RoomActor::client()->create('create arg3');
  \co::sleep(3);
  var_dump(RoomActor::client()->sendAll('sendAll msg'));
  \co::sleep(3);
  var_dump(RoomActor::client()->status());
  \co::sleep(3);
  var_dump(RoomActor::client()->exitAll('sendAll exit'));
});

以上代碼執(zhí)行結果如下:

服務端

php test.php 
string(40) "room actor 00101000000000000000001 start"
string(57) "room actor 00101000000000000000001 onmessage: this is msg"
string(64) "room actor 00101000000000000000001 exit at arg: this is exit arg"
string(40) "room actor 00101000000000000000002 start"
string(40) "room actor 00103000000000000000001 start"
string(57) "room actor 00101000000000000000002 onmessage: sendAll msg"
string(57) "room actor 00103000000000000000001 onmessage: sendAll msg"
string(60) "room actor 00101000000000000000002 exit at arg: sendAll exit"
string(60) "room actor 00103000000000000000001 exit at arg: sendAll exit"

客戶端

php test2.php 
string(23) "00101000000000000000001"
string(19) "reply at 1559623925"
string(18) "exit at 1559623928"
bool(true)
array(3) {
 [1]=>
 int(1)
 [2]=>
 int(0)
 [3]=>
 int(1)
}
bool(true)

更多細節(jié)可以在EasySwoole項目官網得到文檔支持 http://easyswoole.com/

喜歡EasySwoole項目的,可以給個star https://github.com/easy-swoole/easyswoole

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • 淺談并發(fā)處理PHP進程間通信之System V IPC
  • 淺談并發(fā)處理PHP進程間通信之外部介質
  • PHP+Redis鏈表解決高并發(fā)下商品超賣問題(實現(xiàn)原理及步驟)
  • 詳解PHP中curl_multi并發(fā)的實現(xiàn)
  • php多進程并發(fā)編程防止出現(xiàn)僵尸進程的方法分析
  • PHP高并發(fā)和大流量解決方案整理
  • PHP 并發(fā)場景的幾種解決方案
  • php多進程模擬并發(fā)事務產生的問題小結
  • 淺談Swoole并發(fā)編程的魅力

標簽:衡陽 臨沂 銅陵 重慶 麗江 鷹潭 巴彥淖爾 十堰

巨人網絡通訊聲明:本文標題《PHP下用Swoole實現(xiàn)Actor并發(fā)模型的方法》,本文關鍵詞  PHP,下用,Swoole,實現(xiàn),Actor,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP下用Swoole實現(xiàn)Actor并發(fā)模型的方法》相關的同類信息!
  • 本頁收集關于PHP下用Swoole實現(xiàn)Actor并發(fā)模型的方法的相關信息資訊供網民參考!
  • 推薦文章
    石狮市| 利辛县| 深水埗区| 穆棱市| 南昌市| 江西省| 诸城市| 永修县| 政和县| 仲巴县| 什邡市| 弥勒县| 慈溪市| 来宾市| 武强县| 赤壁市| 且末县| 宝清县| 海丰县| 疏附县| 鹿泉市| 嘉善县| 封丘县| 衡东县| 思南县| 当雄县| 华坪县| 彭山县| 云浮市| 宜章县| 砀山县| 成武县| 沂南县| 阿鲁科尔沁旗| 松潘县| 怀安县| 安远县| 随州市| 靖远县| 临沂市| 自贡市|