首页 > 作文

PHP实现简单的协程任务调度demo示例

更新时间:2023-04-08 16:18:23 阅读: 评论:0

本文实例讲述了php实现简单的协程任务调度。分享给大家供大家参考,具体如下:

<?phpclass task{  protected $taskid;  protecte魔枪士加点d $coroutine;  protected $ndvalue = null;  protected $beforefirstyield = true;  public function __construct($taskid, generator $coroutine)  {    $this->taskid = $taskid;    $this->coroutine = $coroutine;  }  public function gettaskid()  {    return $this->taskid;  }  public function tndvalue($ndvalue)  {    $this->ndvalue = $ndvalue;  }  public function run()  {    if ($this->beforefirstyield) {      $this->beforefirstyield = fal;      return $this->coroutine->current();    } el {      $retval = $this->coroutine->nd($this->ndvalue);      $this->ndvalue = null;      return $retval;    }  }  public function isfinished()  {    return !$this->coroutine->valid();  }}class scheduler{  protected $maxtaskid = 0;  protected $taskmap = []; // taskid => task  protected $taskqueue;  public function __construct()  {    $this->taskqueue = new splqueue();电脑麦克风没声音  }  public function newtask(generator $coroutine)  {    $tid = ++$this->maxtaskid;    $task = new task($tid, $coroutine);    $this->taskmap[$tid] = $task;    $this->schedule($task);    return $tid;  }  public function schedule(task $task) 什么叫痔疮图片 {    $this->taskqueue->enqueue($task);  }  public function run()  {    whil义无反顾什么意思e (!$this->taskqueue->impty()) {      $task = $this->taskqueue->dequeue();      $task->run();      if ($task->isfinished()) {        unt($this->taskmap[$task->gettaskid()]);      } el {        $this->schedule($task);      }    }  }}function task1(){  for ($i = 1; $i <= 10; ++$i) {    echo "this is task 1 iteration $i.\n";    sleep(1);    yield;  }}function task2(){  for ($i = 1; $i <= 10; ++$i) {    echo "this is task 2 iteration $i.\n";    陕西大学排名2021最新排名sleep(1);    yield;  }}$scheduler = new scheduler;$scheduler->newtask(task1());$scheduler->newtask(task2());$scheduler->run();

运行结果:

this is task 1 iteration 1.
this is task 1 iteration 2.
this is task 1 iteration 3.
this is task 1 iteration 4.
this is task 1 iteration 5.
this is task 1 iteration 6.
this is task 1 iteration 7.
this is task 1 iteration 8.
this is task 1 iteration 9.
this is task 1 iteration 10.

本文发布于:2023-04-08 16:18:22,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/9646cfb3c6c508650fcb4607e563fc50.html

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

本文word下载地址:PHP实现简单的协程任务调度demo示例.doc

本文 PDF 下载地址:PHP实现简单的协程任务调度demo示例.pdf

相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图