PHP下用Swoole实现Actor并发模型的方法
什么是actor?
actor对于phper来说,可能会比较陌生,写过java的同学会比较熟悉,java一直都有线程的概念(虽然php有pthread,但不普及),它是一种非共享内存的并发模型,每个actor内的数据独立存在,actor之间通过消息传递的形式进行交互调度,且actor是一种高度抽象化的编程模型,非常适合于游戏、硬件行业。
swoole协程与信箱
得益于swoole4.x,我们可以基于swoole的协程与channel快速实现一个信箱模式调度。模拟代码如下:
use swoole\coroutine\channel; go(function (){ //创建十个信箱通道 $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"; } }); } });
以上代码执行输出:
php test.php
actor 8 recv msg : 1559622691
actor 10 recv msg : 1559622693
actor 1 recv msg : 1559622695
actor 5 recv msg : 1559622697
协程通道每次在pop遇到无数据的时候,都会自动让出执行权(具体可以去看swoole协程调度)
actor库
基于上面的原理,我们实行了一个多进程分布的协程actor库
composer require easyswoole/actor=2.x-dev
我们依赖dev库进行测试,生产可以自己依赖stable版本
进程关系
easyswoole的actor模型中,存在两组进程,一组是proxy进程,用来实现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实体被创建的时候,都会执行该回调 var_dump('room actor '.$this->actorid().' start'); } public function onmessage($msg) { //每当一个roomactor实体收到外部消息的时候,都会执行该回调当 var_dump('room actor '.$this->actorid().' onmessage: '.$msg); return 'reply at '.time(); } public function onexit($arg) { //每当一个roomactor实体退出的时候,都会执行该回调 var_dump('room actor '.$this->actorid().' exit at arg: '.$arg); return 'exit at '.time(); } protected function onexception(\throwable $throwable) { //每当一个roomactor出现异常的时候,都会执行该回调 var_dump($throwable->getmessage()); } }
在cli模式下创建一个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"; }
创建一个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')); });
以上代码执行结果如下:
服务端
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)
更多细节可以在easyswoole项目官网得到文档支持
喜欢easyswoole项目的,可以给个star
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
PHP下用rmdir实现删除目录的三种方法小结
-
用windows下编译过的eAccelerator for PHP 5.1.6实现php加速的使用方法
-
PHP下用Swoole实现Actor并发模型的方法
-
用windows下编译过的eAccelerator for PHP 5.1.6实现php加速的使用方法
-
PHP下用rmdir实现删除目录的三种方法小结
-
PHP下用rmdir实现删除目录的三种方法小结
-
用windows下编译过的eAccelerator for PHP 5.1.6实现php加速的使用方法_PHP教程
-
用windows下编译过的eAccelerator for PHP 5.1.6实现php加速的使用方法
-
PHP下用Swoole实现Actor并发模型的方法
-
PHP下用rmdir实现删除目录的三种方法小结_PHP教程