在服務器編程當中,為了實現異步,經常性的需要回調函數,例如以下這段代碼
function send($value) {
$data = process($value);
onReceive($data);
}
function onReceive($recv_value) {
var_dump($recv_value);
}
function process($value) {
return $value+1;
}
$send_value = 1;
send($send_value);
實現的東西很簡單,其實就是將send_value發送到遠端,遠端服務器對其進行加一操作後,發送回來,於是在onReceive中我們可以得到遠端服務器的返回值recv_value。
但是這樣的代碼就會看上去比較支離破碎,尤其是在process當中再次進行遠程過程調用的時候,會變得更加難以開發和維護。協程就是為了解決這樣的問題,使得異步的代碼看起來同步化。
下面就是使用php的yield完成代碼調度的示例(如果想看懂這段代碼,需要首先了解一下php 5.5的新特性generator和yield)
框架代碼如下:
class CCoroutine {
/**
*
* @var Generator
*/
public $coroutine;
/**
*
* @var miexed null or CCoroutine
*/
public $father;
public function __construct($coroutine, $father = null) {
$this->coroutine = $coroutine;
$this->father = $father;
}
}
class AsyncTask {
public $data;
public function __construct($data) {
$this->data = $data;
}
}
abstract class CoroutineScheduler {
protected $coroutine = NULL;
abstract function send_and_receive($value);
public function run($data) {
$co = $this->send_and_receive($data);
$ccoroutine = new CCoroutine($co);
$this->schedule($ccoroutine);
}
protected function schedule($ccoroutine) {
$task = $ccoroutine->coroutine->current();
//如果當前值為空,表示這個$ccoroutine應該已經結束了
if (is_null($task)) {
if (is_null($ccoroutine->father)) {
//已經徹底調度結束了--一般是onRecieve方法運行到最後一步了
return;
} else {
//注意,如果運行到這個分支,則表示子生成器沒有給父生成器傳數據
//子生成器可能是通過引用傳遞來改變父生成器的變量值
//所以這個時候只要調度父生成器就可以了
$ccoroutine->father->coroutine->next();
$father = $ccoroutine->father;
$this->schedule($father);
unset($ccoroutine);
}
} else {
if (is_object($task) && $task instanceof AsyncTask) {
//當task是異步數據請求的時候,開始處理socket並且將進程熄火在這裡
$this->dealTask($task, $ccoroutine);
} elseif (is_object($task) && $task instanceof \Generator) {
//當task是生成器時,表示當前生成器又有了子生成器的調用
$newcc = new CCoroutine($task, $ccoroutine);
$this->schedule($newcc);
} elseif ($ccoroutine->father != null) {
//注意,如果運行到這個分支,則表示在子的生成器裡調用了yield $str;這樣的寫法
//我們規定這種寫法是在給父生成器傳數據,所以當前生成器就會終止調用了轉而去調度父生成器
$ccoroutine->father->coroutine->send($task);
$father = $ccoroutine->father;
$this->schedule($father);
unset($ccoroutine);
}
}
}
protected function dealTask($task, $ccoroutine) {
$this->coroutine = $ccoroutine;
$this->send($task->data);
}
public function send($value) {
$data = $this->process($value);
$this->onReceive($data);
}
public function process($value) {
return $value+1;
}
protected function onReceive($data) {
$this->coroutine->coroutine->send($data);
$this->schedule($this->coroutine);
}
}
調用方代碼如下:
//1. 需要去實現CoroutineScheduler的send_and_receive函數,主要是為了在裡面拿到返回值
class Solution extends CoroutineScheduler {
public function send_and_receive($data) {
$result = (yield new AsyncTask($data));
var_dump($result);
}
}
//2. 在最外層去調用框架的代碼,給出輸入參數 $data
$s = new Solution();
$data = 1;
$s->run($data);