当前位置:首页 > PHP教程 > php高级应用 > 列表

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

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-09 12:03:32 浏览: 评论:0 

这篇文章主要介绍了PHP实现简单的协程任务调度demo,结合实例形式详细分析了PHP基于协程的任务调度基本原理、定义及使用技巧,需要的朋友可以参考下。

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

  1. <?php 
  2. class Task 
  3. { 
  4.   protected $taskId; 
  5.   protected $coroutine; 
  6.   protected $sendValue = null; 
  7.   protected $beforeFirstYield = true; 
  8.   public function __construct($taskId, Generator $coroutine) 
  9.   { 
  10.     $this->taskId = $taskId; 
  11.     $this->coroutine = $coroutine; 
  12.   } 
  13.   public function getTaskId() 
  14.   { 
  15.     return $this->taskId; 
  16.   } 
  17.   public function setSendValue($sendValue) 
  18.   { 
  19.     $this->sendValue = $sendValue; 
  20.   } 
  21.   public function run() 
  22.   { 
  23.     if ($this->beforeFirstYield) { 
  24.       $this->beforeFirstYield = false; 
  25.       return $this->coroutine->current(); 
  26.     } else { 
  27.       $retval = $this->coroutine->send($this->sendValue); 
  28.       $this->sendValue = null; 
  29.       return $retval; 
  30.     } 
  31.   } 
  32.   public function isFinished() 
  33.   { 
  34.     return !$this->coroutine->valid(); 
  35.   } 
  36. } 
  37. class Scheduler 
  38. { 
  39.   protected $maxTaskId = 0; 
  40.   protected $taskMap = []; // taskId => task 
  41.   protected $taskQueue; 
  42.   public function __construct() 
  43.   { 
  44.     $this->taskQueue = new SplQueue(); 
  45.   } 
  46.   public function newTask(Generator $coroutine) 
  47.   { 
  48.     $tid = ++$this->maxTaskId; 
  49.     $task = new Task($tid, $coroutine); 
  50.     $this->taskMap[$tid] = $task; 
  51.     $this->schedule($task); 
  52.     return $tid; 
  53.   } 
  54.   public function schedule(Task $task) 
  55.   { 
  56.     $this->taskQueue->enqueue($task); 
  57.   } 
  58.   public function run() 
  59.   { 
  60.     while (!$this->taskQueue->isEmpty()) { 
  61.       $task = $this->taskQueue->dequeue(); 
  62.       $task->run(); 
  63.       if ($task->isFinished()) { 
  64.         unset($this->taskMap[$task->getTaskId()]); 
  65.       } else { 
  66.         $this->schedule($task); 
  67.       } 
  68.     } 
  69.   } 
  70. } 
  71. function task1() 
  72. { 
  73.   for ($i = 1; $i <= 10; ++$i) { 
  74.     echo "This is task 1 iteration $i.\n"; 
  75.     sleep(1); 
  76.     yield; 
  77.   } 
  78. } 
  79. function task2() 
  80. { 
  81.   for ($i = 1; $i <= 10; ++$i) { 
  82.     echo "This is task 2 iteration $i.\n"; 
  83.     sleep(1); 
  84.     yield; 
  85.   } 
  86. } 
  87. $scheduler = new Scheduler; 
  88. $scheduler->newTask(task1()); 
  89. $scheduler->newTask(task2()); 
  90. $scheduler->run(); 

运行结果:

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

Tags: PHP协程任务 demo

分享到: