2015-05-27
在服务器编程当中,为了实现异步,经常性的需要回调函数,例如以下这段代码
function send($value) { $data = process($value); onReceive($data);}function onReceive($recv_value) { var_dump($recv_value);}function process($value) { return $value+1;}$send_value = 1;send($send_value);
实现的东西很简单,其实就是将send_value发送到远端,远端服务器对其进行加一操作后,发送回来,于是在onReceive中我们可以得到远端服务器的返回值recv_value。
但是这样的代码就会看上去比较支离破碎,尤其是在process当中再次进行远程过程调用的时候,会变得更加难以开发和维护。协程就是为了解决这样的问题,使得异步的代码看起来同步化。
下面就是使用php的yield完成代码调度的示例(如果想看懂这段代码,需要首先了解一下php 5.5的新特性generator和yield)
框架代码如下:
class CCoroutine { /** * * @var Generator */ public $coroutine; /** * * @var miexed null or CCoroutine */ public $father; public function __construct($coroutine, $father = null) { $this->coroutine = $coroutine; $this->father = $father; }}class AsyncTask { public $data; public function __construct($data) { $this->data = $data; }}abstract class CoroutineScheduler { protected $coroutine = NULL; abstract function send_and_receive($value); public function run($data) { $co = $this->send_and_receive($data); $ccoroutine = new CCoroutine($co); $this->schedule($ccoroutine); } protected function schedule($ccoroutine) { $task = $ccoroutine->coroutine->current(); //如果当前值为空,表示这个$ccoroutine应该已经结束了 if (is_null($task)) { if (is_null($ccoroutine->father)) { //已经彻底调度结束了--一般是onRecieve方法运行到最后一步了 return; } else { //注意,如果运行到这个分支,则表示子生成器没有给父生成器传数据 //子生成器可能是通过引用传递来改变父生成器的变量值 //所以这个时候只要调度父生成器就可以了 $ccoroutine->father->coroutine->next(); $father = $ccoroutine->father; $this->schedule($father); unset($ccoroutine); } } else { if (is_object($task) && $task instanceof AsyncTask) { //当task是异步数据请求的时候,开始处理socket并且将进程熄火在这里 $this->dealTask($task, $ccoroutine); } elseif (is_object($task) && $task instanceof /Generator) { //当task是生成器时,表示当前生成器又有了子生成器的调用 $newcc = new CCoroutine($task, $ccoroutine); $this->schedule($newcc); } elseif ($ccoroutine->father != null) { //注意,如果运行到这个分支,则表示在子的生成器里调用了yield $str;这样的写法 //我们规定这种写法是在给父生成器传数据,所以当前生成器就会终止调用了转而去调度父生成器 $ccoroutine->father->coroutine->send($task); $father = $ccoroutine->father; $this->schedule($father); unset($ccoroutine); } } } protected function dealTask($task, $ccoroutine) { $this->coroutine = $ccoroutine; $this->send($task->data); } public function send($value) { $data = $this->process($value); $this->onReceive($data); } public function process($value) { return $value+1; } protected function onReceive($data) { $this->coroutine->coroutine->send($data); $this->schedule($this->coroutine); }}
调用方代码如下:
//1. 需要去实现CoroutineScheduler的send_and_receive函数,主要是为了在里面拿到返回值class Solution extends CoroutineScheduler { public function send_and_receive($data) { $result = (yield new AsyncTask($data)); var_dump($result); }}//2. 在最外层去调用框架的代码,给出输入参数 $data$s = new Solution();$data = 1;$s->run($data);
1
CI框架连接数据库配置操作以及多数据库操作
09-05
2
asp 简单读取数据表并列出来 ASP如何快速从数据库读取大量数据
05-17
3
C语言关键字及其解释介绍 C语言32个关键字详解
04-05
4
C语言中sizeof是什么意思 c语言里sizeof怎样用法详解
04-26
5
PHP中的魔术方法 :__construct, __destruct , __call, __callStatic,__get, __set, __isset, __unset , __sleep,
09-05
6
最简单的asp登陆界面代码 asp登陆界面源代码详细介绍
04-12
7
PHP中的(++i)前缀自增 和 (i++)后缀自增
09-05
8
常用dos命令及语法
09-27
PHP中include和require区别之我见
2014-09-05
将视频设置为Android手机开机动画的教程
2014-12-11
php递归返回值的问题
2014-09-05
如何安装PHPstorm并配置方法教程 phpstorm安装后要进行哪些配置
2017-05-03
PHP 教程之如何使用BLOB存取图片信息实例
2014-09-05
java中的info是什么意思
2022-03-24
单片机编程好学吗?单片机初学者怎样看懂代码
2022-03-21
学ug编程如何快速入门?
2022-03-17
PHP数组函数array
2014-09-05
IcePHP框架中的快速后台中的通用CRUD功能框架
2014-09-05
真实枪械武器模拟器游戏下载v2.177 安卓版
射击枪战 194.6M
下载果汁四溅2中文版下载v1.9.8 安卓版
休闲益智 81.7M
下载我飞刀玩得贼6手游下载v2.2.7 安卓版
休闲益智 73.9M
下载方块冒险下载v1.1.9 安卓版
休闲益智 19.7M
下载我是鸽手游戏下载v2.0.0 安卓版
休闲益智 41.3M
下载耶小兔子2最新中文版下载v1.2.9 安卓版
休闲益智 60.9M
下载弓箭大师你就是王者游戏下载v2.0.1 安卓最新版
其它手游 164.4M
下载弓箭大师你就是王者官方版下载v2.0.1 安卓版
其它手游 164.4M
下载3d狙击王者手机游戏下载v1.5 安卓版
下载
足球冲鸭手机版下载v1.0.16.404.401.0116 安卓版
下载
有种来找我手游下载v1.4.8 安卓版
下载
忍者跳跳跳官方版下载v1.0.2 安卓版
下载
百战斗斗堂拇指游戏下载v5.8 安卓版
下载
梦幻斗斗堂手游下载v5.8 安卓版
下载
百战斗斗堂4399版下载v5.8 安卓版
下载
百战斗斗堂九游手游下载v5.8 安卓版
下载