PHP下用Swoole实现Actor并发模型的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-11-25 15:02:59 浏览: 评论:0
这篇文章主要介绍了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项目官网得到文档支持 http://easyswoole.com/
喜欢EasySwoole项目的,可以给个star https://github.com/easy-swoole/easyswoole
Tags: Swoole Actor
相关文章
- ·php异步多线程swoole使用实例程序(2015-04-15)
- ·PHP程序员学习使用Swoole的理由(2018-09-14)
- ·swoole_process父子进程管道通信的代码示例(2020-01-16)
- ·swoole事件处理流程(2020-04-06)
- ·Swoole协程与传统fpm同步模式比较(2020-04-09)
- ·PHP框架Swoole定时器Timer特性分析(2021-04-05)
- ·php异步多线程swoole用法实例(2021-04-26)
- ·使用php+swoole对client数据实时更新(一)(2021-07-04)
- ·PHP+swoole实现简单多人在线聊天群发(2021-07-06)
- ·基于 Swoole 的微信扫码登录功能实现代码(2021-08-30)
- ·基于swoole实现多人聊天室(2021-10-01)
- ·swoole_process实现进程池的方法示例(2021-11-01)
- ·Swoole实现异步投递task任务案例详解(2021-11-14)
- ·PHP扩展Swoole实现实时异步任务队列示例(2021-11-17)
- ·PHP+swoole+linux实现系统监控和性能优化操作示例(2021-11-17)
- ·Swoole4.4协程抢占式调度器详解(2021-11-23)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)