PHP中使用協同程序實現合作多任務(2)_PHP教程
推薦:php修改NetBeans默認字體的大小在Netbeans中由于使用了Swing進行開發,所以其中界面的字體也是由Java虛擬機進行配置而不是隨操作系統的。在安裝完Netbeans后默認的字體大小是11px。而在Windows下的宋體最小支持12px。所以字體為11px就已經無法完整顯示了。 簡單的解決辦法就是將字體改大一點。詳細的
如果閱讀了上面的logger()例子,那么你認為“為了雙向通信我為什么要使用協程呢? 為什么我不能只用常見的類呢?”,你這么問完全正確。上面的例子演示了基本用法,然而上下文中沒有真正的展示出使用協程的優點。這就是列舉許多協程例子的 理由。正如上面介紹里提到的,協程是非常強大的概念,不過這樣的應用很稀少而且常常十分復雜。給出一些簡單而真實的例子很難。
在這篇文章里,我決定去做的是使用協程實現多任務協作。我們盡力解決的問題是你想并發地運行多任務(或者“程序”)。不過處理器在一個時刻只能運行 一個任務(這篇文章的目標是不考慮多核的)。因此處理器需要在不同的任務之間進行切換,而且總是讓每個任務運行 “一小會兒”。
多任務協作這個術語中的“協作”說明了如何進行這種切換的:它要求當前正在運行的任務自動把控制傳回給調度器,這樣它就可以運行其他任務了。這與 “搶占”多任務相反,搶占多任務是這樣的:調度器可以中斷運行了一段時間的任務,不管它喜歡還是不喜歡。協作多任務在Windows的早期版本 (windows95)和Mac OS中有使用,不過它們后來都切換到使用搶先多任務了。理由相當明確:如果你依靠程序自動傳回 控制的話,那么壞行為的軟件將很容易為自身占用整個CPU,不與其他任務共享。
這個時候你應當明白協程和任務調度之間的聯系:yield指令提供了任務中斷自身的一種方法,然后把控制傳遞給調度器。因此協程可以運行多個其他任務。更進一步來說,yield可以用來在任務和調度器之間進行通信。
我們的目的是 對 “任務”用更輕量級的包裝的協程函數:
<?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();
}
}
一個任務是用 任務ID標記一個協程。使用setSendValue()方法,你可以指定哪些值將被發送到下次的恢復(在之后你會了解到我們需要這個)。 run()函數確實沒有做什么,除了調用send()方法的協同程序。要理解為什么添加beforeFirstYieldflag,需要考慮下面的代碼片 段:
復制代碼 代碼如下:<?php
function gen() {
yield 'foo';
yield 'bar';
}
$gen = gen();
var_dump($gen->send('something'));
// As the send() happens before the first yield there is an implicit rewind() call,
// so what really happens is this:
$gen->rewind();
var_dump($gen->send('something'));
// The rewind() will advance to the first yield (and ignore its value), the send() will
// advance to the second yield (and dump its value). Thus we loose the first yielded value!
通過添加 beforeFirstYieldcondition 我們可以確定 first yield 的值 被返回。
調度器現在不得不比多任務循環要做稍微多點了,然后才運行多任務:
<?php
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);
}
分享:PHP刪除數組中特定元素的兩種方法這篇文章介紹了PHP中刪除數組中特定元素的兩種方法,有需要的朋友可以參考一下 方法一: 復制代碼 代碼如下: ?php $arr1 = array(1,3, 5,7,8); $key = array_search(3, $arr1); if ($key !== false) array_splice($arr1, $key, 1); var_dump($arr1); ? 輸出: array(4)
- 相關鏈接:
- 教程說明:
PHP教程-PHP中使用協同程序實現合作多任務(2)。