PHP實現協程_PHP教程

來源:互聯網
上載者:User

PHP實現協程


在伺服器編程當中,為了實現非同步,經常性的需要回呼函數,例如以下這段代碼

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);    }}


架構將 send, onReceive等函數全部都封裝好了,使得調用方的代碼看起來可以是同步的代碼

調用方代碼如下:

//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);



http://www.bkjia.com/PHPjc/979823.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/979823.htmlTechArticlePHP實現協程 在伺服器編程當中,為了實現非同步,經常性的需要回呼函數,例如以下這段代碼 function send($value) { $data = process($value); onReceive...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.