PHP實(shí)現(xiàn)簡(jiǎn)單的協(xié)程任務(wù)調(diào)度demo示例
本文實(shí)例講述了PHP實(shí)現(xiàn)簡(jiǎ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();
運(yùn)行結(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ù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php使用gearman進(jìn)行任務(wù)分發(fā)操作實(shí)例詳解
- python基于json文件實(shí)現(xiàn)的gearman任務(wù)自動(dòng)重啟代碼實(shí)例
- PHP并發(fā)多進(jìn)程處理利器Gearman使用介紹
- Gearman::XS在Centos下的編譯安裝方法
- gearman隊(duì)列持久化引發(fā)的問題及解決方法
- gearman的安裝啟動(dòng)及python API使用實(shí)例
- rhel5.7下安裝gearmand及啟動(dòng)的方法
- PHP 進(jìn)程池與輪詢調(diào)度算法實(shí)現(xiàn)多任務(wù)的示例代碼
- PHP 多進(jìn)程與信號(hào)中斷實(shí)現(xiàn)多任務(wù)常駐內(nèi)存管理實(shí)例方法
- php解決crontab定時(shí)任務(wù)不能寫入文件問題的方法分析
- gearman管理工具GearmanManager的安裝與php使用方法示例
相關(guān)文章
PHP開發(fā)規(guī)范手冊(cè)之PHP代碼規(guī)范詳解
對(duì)于PHP入門學(xué)習(xí)的童鞋來說,了解PHP開發(fā)規(guī)范可以少走很多彎路,網(wǎng)上各種PHP開發(fā)規(guī)范也很多,我結(jié)合自身使用PHP的情況,來說說我所理解的PHP開發(fā)規(guī)范。2011-01-01
smarty實(shí)現(xiàn)多級(jí)分類的方法
這篇文章主要介紹了smarty實(shí)現(xiàn)多級(jí)分類的方法,涉及循環(huán)讀取的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
如何使用Laravel Eloquent來開發(fā)無限極分類
在網(wǎng)上商城上,我們經(jīng)??梢钥吹蕉嗉?jí)分類、子分類、甚至無限極分類。本文將向你展示如何優(yōu)雅的通過 Laravel Eloquent 將其實(shí)現(xiàn)。2021-05-05
php將數(shù)組轉(zhuǎn)換成csv格式文件輸出的方法
這篇文章主要介紹了php將數(shù)組轉(zhuǎn)換成csv格式文件輸出的方法,涉及php操作csv文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
PHP和JavaScrip分別獲取關(guān)聯(lián)數(shù)組的鍵值示例代碼
關(guān)聯(lián)數(shù)組的鍵值獲取,有很多方法,在本文為大家介紹下PHP和JavaScrip中時(shí)如何實(shí)現(xiàn)的,感興趣的朋友可以參考下2013-09-09
joomla jce editor 解決上傳中文名文件失敗問題
要么上傳失敗,要么傳上去了中文名不能被web訪問,研究了下JCE的源碼,改掉保存文件名的方式,使用純數(shù)字來作為文件名,具體修改如下,遇到類型情況的朋友可以參考下哈2013-06-06

