PHP实现协同程序

简介:

于server其中编程。为了实现异步。通常情况下,需要回调。比例如下面的代码

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);



 
 
实现的东西非常easy,事实上就是将send_value发送到远端,远端server对其进行加一操作后,发送回来,于是在onReceive中我们能够得到远端server的返回值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);
    }

}


框架将 send, onReceive等函数所有都封装好了,使得调用方的代码看起来能够是同步的代码

调用方代码例如以下:

//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);





版权声明:本文博客原创文章,博客,未经同意,不得转载。



本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4719265.html,如需转载请自行联系原作者


相关文章
|
1月前
|
缓存 PHP 数据库
PHP程序性能优化指南
在当今互联网快速发展的时代,PHP作为一种流行的服务器端脚本语言,其性能优化显得尤为重要。本文将介绍一些提升PHP程序性能的有效方法,帮助开发者更好地优化他们的代码,提升应用程序的响应速度和效率。
|
3月前
|
PHP
HYBBS 表白墙网站PHP程序源码 可封装成APP
HYBBS 表白墙网站PHP程序源码 可封装成APP
33 1
|
PHP
php : 无法将“php”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
php : 无法将“php”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
1812 0
php : 无法将“php”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
|
9月前
|
SQL JSON 关系型数据库
php执行语句在MySQL批量插入大数据量的解决方案及计算程序执行时间(大数据量、MySQL语句优化)
php执行语句在MySQL批量插入大数据量的解决方案及计算程序执行时间(大数据量、MySQL语句优化)
196 1
|
9月前
|
存储 JSON 供应链
使用PHP构建区块链应用程序
区块链技术作为一种去中心化的分布式账本系统,已经引起了广泛的关注。它具有安全、透明、可追溯等特点,被广泛应用于加密货币、供应链管理、身份验证等领域。本文将介绍如何使用PHP构建一个简单的区块链应用程序,并提供相应的代码实现、运行结果和解读。
139 0
|
10月前
|
安全 程序员 网络安全
PHP 恶意程序简单分析
PHP 恶意程序简单分析
82 0
|
10月前
|
自然语言处理 前端开发 小程序
淘宝代购系统;海外代购系统;代购程序,代购系统源码PHP前端源码
代购业务近年兴起的一种购物模式,是帮国外客户购买中国商品。主要通过外贸代购模式,把淘宝、 天猫等电商平台的全站商品通过API接入到你的网站上,瞬间就可以架设一个有数亿产品的大型网上商城,而且可以 把这些中文的商品全部自动翻译成各国语言,能让国外客户看懂,直接在网站上下单,然后网站运营方代为购买再邮
|
应用服务中间件 Linux PHP
xdebug3+php8.0+vscode断点调试php程序
xdebug3+php8.0+vscode断点调试php程序
358 0