欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2019年06月12日 09:17:36   作者:如果的如果  
這篇文章主要介紹了PHP下用Swoole實現(xiàn)Actor并發(fā)模型的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

什么是Actor?

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

Swoole協(xié)程與信箱

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

use Swoole\Coroutine\Channel;
go(function (){
  //創(chuàng)建十個信箱通道
  $mailBoxes = [];
  for ($i = 1;$i <= 10;$i++){
    $mailBoxes[$i] = new Channel(16);
  }
  //模擬master 郵局調(diào)度,隨機(jī)像一個信箱投遞消息
  go(function ()use($mailBoxes){
    while (1){
      \co::sleep(2);
      $key = rand(1,10);
      ($mailBoxes[$key])->push(time());
    }
  });
  //模擬actor 實體消費(fèi)
  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í)行權(quán)(具體可以去看Swoole協(xié)程調(diào)度)

Actor庫

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

composer require easyswoole/actor=2.x-dev

我們依賴dev庫進(jìn)行測試,生產(chǎn)可以自己依賴stable版本

進(jìn)程關(guān)系

Easyswoole的Actor模型中,存在兩組進(jìn)程,一組是proxy進(jìn)程,用來實現(xiàn)Actor對外服務(wù),一組是worker進(jìn)程,proxy進(jìn)程與worker進(jìn)程之間通過unixsock進(jìn)行通訊,而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()
  {
    //每當(dāng)一個RoomActor實體被創(chuàng)建的時候,都會執(zhí)行該回調(diào)
    var_dump('room actor '.$this->actorId().' start');
  }
  public function onMessage($msg)
  {
    //每當(dāng)一個RoomActor實體收到外部消息的時候,都會執(zhí)行該回調(diào)當(dāng)
    var_dump('room actor '.$this->actorId().' onmessage: '.$msg);
    return 'reply at '.time();
  }
  public function onExit($arg)
  { 
    //每當(dāng)一個RoomActor實體退出的時候,都會執(zhí)行該回調(diào)
    var_dump('room actor '.$this->actorId().' exit at arg: '.$arg);
    return 'exit at '.time();
  }
  protected function onException(\Throwable $throwable)
  {
    //每當(dāng)一個RoomActor出現(xiàn)異常的時候,都會執(zhí)行該回調(diào)
    var_dump($throwable->getMessage());
  }
}

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

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í)行結(jié)果如下:

服務(wù)端

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)

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • phpfans留言版用到的install.php

    phpfans留言版用到的install.php

    phpfans留言版用到的install.php...
    2007-01-01
  • 一個完整的php文件上傳類實例講解

    一個完整的php文件上傳類實例講解

    這篇文章主要介紹了一個完整的php文件上傳類實例,可以自定上傳文件大小與上傳文件類型及文件保存地址,需要的朋友可以參考下
    2015-10-10
  • PHP控制前臺彈出對話框的實現(xiàn)方法

    PHP控制前臺彈出對話框的實現(xiàn)方法

    本文給大家分享通過php echo出javascript腳本來控制前臺彈出對話框的效果,非常不錯,具有參考借鑒價值,感興趣的朋友一起看下吧
    2016-08-08
  • PHP捕獲Fatal error錯誤的方法

    PHP捕獲Fatal error錯誤的方法

    這篇文章主要介紹了PHP捕獲Fatal error錯誤的方法,使用register_shutdown_function來捕獲Fatal error錯誤,需要的朋友可以參考下
    2014-06-06
  • PHP實現(xiàn)簡易圖形計算器

    PHP實現(xiàn)簡易圖形計算器

    這篇文章主要為大家詳細(xì)介紹了PHP實現(xiàn)簡易圖形計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • laravel自定義分頁的實現(xiàn)案例offset()和limit()

    laravel自定義分頁的實現(xiàn)案例offset()和limit()

    今天小編就為大家分享一篇laravel自定義分頁的實現(xiàn)案例offset()和limit(),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • smarty簡單入門實例

    smarty簡單入門實例

    這篇文章主要介紹了smarty簡單入門實例,包括了配置文件的用法與模板文件的使用,非常具有實用價值,需要的朋友可以參考下
    2014-11-11
  • PHP圖片水印類的封裝

    PHP圖片水印類的封裝

    這篇文章主要為大家詳細(xì)介紹了PHP圖片水印類的封裝,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • php GUID生成函數(shù)和類

    php GUID生成函數(shù)和類

    這篇文章主要介紹了使用php生成GUID的方法,分別使用了函數(shù)和類的方式生成GUID,詳細(xì)介紹了什么是GUID、GUID的優(yōu)點(diǎn)等,需要的朋友可以參考下
    2014-03-03
  • PHP設(shè)計模式入門之狀態(tài)模式原理與實現(xiàn)方法分析

    PHP設(shè)計模式入門之狀態(tài)模式原理與實現(xiàn)方法分析

    這篇文章主要介紹了PHP設(shè)計模式入門之狀態(tài)模式,結(jié)合實例形式分析了PHP狀態(tài)模式基本概念、原理、實現(xiàn)方法及操作注意事項,需要的朋友可以參考下
    2020-04-04

最新評論