什么是actor?
actor对于phper来说,可能会比较陌生,写过java的同学会比较熟悉,java一直都有线程的概念(虽然php有pthread,但不普及),它是一种非共享内存的并发模型,每个actor内的数据独立存在,actor之间通过消息传递的形式进行交互调度,且actor是一种高度抽象化的编程模型,非常适合于游戏、硬件行业。
swoole最好的专业协程与信箱
得益于swoole4.x,我们可以基于swoole的协程与channel快速实现一个信箱模式调度。模拟代码如下:
u swoole\coroutine\channel;go(function (){ //创建十个信箱通道 $mailboxes = []; for ($i = 1;$i <= 10;$i++){ $mailboxes[$i] = new channel(16); } //模拟master 邮局调度,随机像一个信箱投递消息 go(function ()u($mailboxes){ while (1){ \co::sleep(2); $key = rand(1,10); ($mailboxes[$key])->push(time()); } }); //模拟actor 实体消费 for ($i = 1;$i <= 10;$i++){ 儿童视力标准 go(function ()u($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库
compor require easyswoole/actor=2.x-dev
我们依赖dev库进行测试,生产可以自己依赖stable版本
进程关系
easyswoole的actor模型中,存在两组进程,一组是proxy进程,用来实现actor对外服务,一组是worker进程,proxy进程与worker进程之间通过unixsock进行通讯,而actor实例就均匀的分布worker之中。
样例代码
比如在一个聊天室中,我们可以定义一个房间模型。
namespace easyswoole\actor\test;u easyswoole\actor\abstractactor;u easyswoole\actor\actorconfig;class roomactor extends abstractactor{ public static function configure(actorconfig $actorconfig) { $actorconfig->tactorname('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(); } publi特种动物养殖c 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服务
u easyswoole\actor\actor;u easyswoole\actor\test\roomactor;u easyswoole\actor\pr初一英语语法oxyprocess;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测试脚本
u easyswoole\actor\actor;u 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()->nd($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()->ndall('ndall msg')); \co::sleep(3); var_dump(roomactor::client()->status()); \co::sleep(3); var_dump(roomactor::client()->exitall('ndall 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: ndall msg"string(57) "room actor 00103000000000000000001 onmessage: ndall msg"string(60) "room actor 00101000000000000000002 exit at arg: ndall exit"string(60) "room actor 00103000000000000000001 exit at arg: ndall 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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持www.887551.com。
本文发布于:2023-04-07 07:57:43,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/26bf045a9716506234fb7183cd54f92e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHP下用Swoole实现Actor并发模型的方法.doc
本文 PDF 下载地址:PHP下用Swoole实现Actor并发模型的方法.pdf
留言与评论(共有 0 条评论) |