首页 > 作文

php基于协程实现异步的方法分析

更新时间:2023-04-07 14:39:27 阅读: 评论:0

本文实例讲述了php基于协程实现异步的方法。分享给大家供大家参考,具体如下:

github上php的协程大部分是根据这篇文章实现的:http://nikic.github.io/2012/12/22/cooperative-multitasking-using-coroutines-in-php.html

它们最终的结果都是把回调变成了优雅的顺序执行的代码,但还是阻塞的,不是真正的异步。

比如最热门的:https://github.com/recoilphp/recoil

先安装最亲爱的妈妈:

compor require recoil/recoil

执行:

<?php//recoil.phpinclude __dir__ . '/vendor/autoload.php';u recoil\react\reactkernel;$i = 100000;reactkernel::start(task1());reactkernel::start(task2());function task1(){  global $i;  echo "wait start" . php_eol;  while ($i-- > 0) {    yield;  }  echo "wait end" . php_eol;};function task2(){  echo "hello " . php_eol;  yield;  echo "world!" . php_eol;}

结果:

wait start
//等待若干秒
wait end
hello
world!

我本来是想让两个任务并行,结果两个任务变成了串行,中间等待的时间什么事情都干不了。react响应式的编程是严格禁止这种等待的,所以我就参照unity3d的协程自己写了个php版本的。上代码:

<?php//coroutine.php//依赖swoole实现的定时器,也可以用其它方法实现定时器class coroutine{  //可以根据需要更改定时器间隔,单位ms  const tick_interval = 1;  private $routinelist;  private $tickid = -1;  public function __construct()  {    $this->routinelist = [];  }  public function start(generator $routine)  {    $task = new task($routine);    $this->routinelist[] = $task;    $this->starttick();  }  public function stop(generator $routine)  {    foreach ($this->routinelist as $k => $task) {      if($task->getroutine() == $routine){        unt($this->routinelist[$k]);      }    }  }  private function starttick()  {    swoole_timer_tick(lf::tick_interval, function($timerid){      $this->tickid = $timerid;      $this->run();    });  }  private fun出自幽谷ction stoptick()  {    if($this->tickid >= 0) {      swoole_timer_clear($this->tickid未来行业前景排名);    }  }  private function run()  {    if(empty($this->routinelist)){      $this->stoptick();      return;    }    foreach ($this->routinelist as $k => $task) {      $task->run();      if($task->isfinished()){        unt($this->routinelist[$k]);      }    }  }  }class task{  protected $stack;  protected $routine;  public function __construct(generator $routine)  {    $this->routine = $routine;    $this->stack = new splstack();  }  /**   * [run 协程调度]   * @return [type]     [description]   */  public function run()  {    $routine = &$this->routine;    try {      if(!$routine){        return;      }      $value = $routine->current();      //嵌套的协程      if ($value instanc烟花易冷被禁eof g闪闪烁烁的意思enerator) {        $this->stack->push($routine);        $routine = $value;        return;      }      //嵌套的协程返回      if(!$routine->valid() && !$this->stack->impty()) {        $routine = $this->stack->pop();      }      $routine->next();    } catch (exception $e) {      if ($this->stack->impty()) {        /*          throw the exception        */        return;      }    }  }  /**   * [isfinished 判断该task是否完成]   * @return boolean [description]   */  public function isfinished()  {    return $this->stack->impty() && !$this->routine->valid();  }  public function getroutine()  {    return $this->routine;  }}

测试代码:

<?php//test.php require 'coroutine.php';$i = 10000;$c = new coroutine();$c->start(task1());$c->start(task2());function task1(){  global $i;  echo "wait start" . php_eol;  while ($i-- > 0) {    yield;  }  echo "wait end" . php_eol;};function task2(){  echo "hello " . php_eol;  yield;  echo "world!" . php_eol;}

结果:

wait start
hello
world!
//等待几秒,但不阻塞
wait end

本文发布于:2023-04-07 14:38:23,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/3bdbf828b773a1177a50adb1f4421b68.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:php基于协程实现异步的方法分析.doc

本文 PDF 下载地址:php基于协程实现异步的方法分析.pdf

标签:定时器   嵌套   代码   都是
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图