濮阳杆衣贸易有限公司

主頁 > 知識庫 > PHP實現(xiàn)簡單的協(xié)程任務(wù)調(diào)度demo示例

PHP實現(xiàn)簡單的協(xié)程任務(wù)調(diào)度demo示例

熱門標(biāo)簽:昭通辦理400電話 岳陽外呼型呼叫中心系統(tǒng)在哪里 山西回?fù)芡夂粝到y(tǒng) 百應(yīng)電話機器人服務(wù) 騰訊外呼管理系統(tǒng) 青島語音外呼系統(tǒng)招商 河南電銷卡外呼系統(tǒng)哪家強 揚州地圖標(biāo)注app 山西探意電話機器人

本文實例講述了PHP實現(xiàn)簡單的協(xié)程任務(wù)調(diào)度。分享給大家供大家參考,具體如下:

?php
class Task
{
  protected $taskId;
  protected $coroutine;
  protected $sendValue = null;
  protected $beforeFirstYield = true;
  public function __construct($taskId, Generator $coroutine)
  {
    $this->taskId = $taskId;
    $this->coroutine = $coroutine;
  }
  public function getTaskId()
  {
    return $this->taskId;
  }
  public function setSendValue($sendValue)
  {
    $this->sendValue = $sendValue;
  }
  public function run()
  {
    if ($this->beforeFirstYield) {
      $this->beforeFirstYield = false;
      return $this->coroutine->current();
    } else {
      $retval = $this->coroutine->send($this->sendValue);
      $this->sendValue = 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()
  {
    while (!$this->taskQueue->isEmpty()) {
      $task = $this->taskQueue->dequeue();
      $task->run();
      if ($task->isFinished()) {
        unset($this->taskMap[$task->getTaskId()]);
      } else {
        $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";
    sleep(1);
    yield;
  }
}
$scheduler = new Scheduler;
$scheduler->newTask(task1());
$scheduler->newTask(task2());
$scheduler->run();

運行結(jié)果:

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.

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP進(jìn)程與線程操作技巧總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家PHP程序設(shè)計有所幫助。

您可能感興趣的文章:
  • php使用gearman進(jìn)行任務(wù)分發(fā)操作實例詳解
  • python基于json文件實現(xiàn)的gearman任務(wù)自動重啟代碼實例
  • PHP并發(fā)多進(jìn)程處理利器Gearman使用介紹
  • Gearman::XS在Centos下的編譯安裝方法
  • gearman隊列持久化引發(fā)的問題及解決方法
  • gearman的安裝啟動及python API使用實例
  • rhel5.7下安裝gearmand及啟動的方法
  • PHP 進(jìn)程池與輪詢調(diào)度算法實現(xiàn)多任務(wù)的示例代碼
  • PHP 多進(jìn)程與信號中斷實現(xiàn)多任務(wù)常駐內(nèi)存管理實例方法
  • php解決crontab定時任務(wù)不能寫入文件問題的方法分析
  • gearman管理工具GearmanManager的安裝與php使用方法示例

標(biāo)簽:宜賓 銅川 南陽 湛江 黃南 婁底 寶雞 鎮(zhèn)江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實現(xiàn)簡單的協(xié)程任務(wù)調(diào)度demo示例》,本文關(guān)鍵詞  PHP,實現(xiàn),簡單,的,協(xié)程,任務(wù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《PHP實現(xiàn)簡單的協(xié)程任務(wù)調(diào)度demo示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于PHP實現(xiàn)簡單的協(xié)程任務(wù)調(diào)度demo示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    宝兴县| 望奎县| 扶沟县| 什邡市| 彭阳县| 和田市| 大理市| 封开县| 五常市| 红河县| 西乌珠穆沁旗| 鱼台县| 平乡县| 新营市| 澎湖县| 新余市| 精河县| 高州市| 富川| 济阳县| 仁化县| 克拉玛依市| 固镇县| 永康市| 崇阳县| 汤阴县| 佛坪县| 合肥市| 诸暨市| 武陟县| 葫芦岛市| 丰县| 赞皇县| 金溪县| 太湖县| 四平市| 琼结县| 通化县| 自贡市| 道真| 沽源县|