PHP協程初體驗

來源:互聯網
上載者:User
PHP協程初體驗

By warezhou 2014.11.24

上次通過C擴充為PHP添加coroutine嘗試失敗之後,由於短期內啃下Zend可能性幾乎為零,只能打語言原生能力的主意了。Google之後發現,PHP5.5引入了Generator和Coroutine新特性,於是才有了本文的誕生。

背景閱讀

《當C/C++後台開發遇上Coroutine》

http://km.oa.com/group/906/articles/show/165396

《一次失敗的PHP擴充開發之旅》

http://km.oa.com/group/906/articles/show/208269

預備知識

Generator

function my_range($start, $end, $step = 1) {    for ($i = $start; $i <= $end; $i += $step) {        yield $i;    }}foreach (my_range(1, 1000) as $num) {    echo $num, "\n";}/* * 1 * 2 * ... * 1000 */

圖 1 基於generator的range()實現

$range = my_range(1, 1000);var_dump($range);/* * object(Generator)#1 (0) { * } */var_dump($range instanceof Iterator);/* * bool(true) */

圖 2 my_range()的實現推測

由於接觸PHP時日尚淺,並未深入語言實現細節,所以只能根據現象進行猜測,以下是我的一些個人理解:

  • 包含yield關鍵字的函數比較特殊,傳回值是一個Generator對象,此時函數內語句尚未真正執行
  • Generator對象是Iterator介面執行個體,可以通過rewind()、current()、next()、valid()系列介面進行操縱
  • Generator可以視為一種“可中斷”的函數,而yield構成了一系列的“中斷點”
  • Generator類似於車間生產的流水線,每次需要用產品的時候才從那裡取一個,然後這個流水線就停在那裡等待下一次取操作
  • Coroutine

    細心的讀者可能已經發現,截至目前,其實Generator已經實現了Coroutine的關鍵特性:中斷執行、恢複執行。按照《當C/C++後台開發遇上Coroutine》的思路,藉助“全域變數”一類語言設施進行資訊傳遞,實現非同步Server應該足夠了。

    其實相對於swapcontext族函數,Generator已經前進了一大步,具備了“返回資料”的能力,如果同時具備“發送資料”的能力,就再也不必通過那些蹩腳的手法繞路而行了。在PHP裡面,通過Generator的send()介面(注意:不再是next()介面),可以完成“發送資料”的任務,從而實現了真正的“雙向通訊”。

    function gen() {    $ret = (yield 'yield1');    echo "[gen]", $ret, "\n";    $ret = (yield 'yield2');    echo "[gen]", $ret, "\n";}$gen = gen();$ret = $gen->current();echo "[main]", $ret, "\n";$ret = $gen->send("send1");echo "[main]", $ret, "\n";$ret = $gen->send("send2");echo "[main]", $ret, "\n";/* * [main]yield1 * [gen]send1 * [main]yield2 * [gen]send2 * [main] */

    圖 3 Coroutine雙向通訊樣本

    作為C/C++系碼農,發現“可重新進入”、“雙向通訊”能力之後,貌似沒有更多奢求了,不過PHP還是比較慷慨,繼續添加了Exception機制,“錯誤處理”機製得到進一步完善。

    function gen() {    $ret = (yield 'yield1');    echo "[gen]", $ret, "\n";    try {        $ret = (yield 'yield2');        echo "[gen]", $ret, "\n";    } catch (Exception $ex) {        echo "[gen][Exception]", $ex->getMessage(), "\n";    }       echo "[gen]finish\n";}$gen = gen();$ret = $gen->current();echo "[main]", $ret, "\n";$ret = $gen->send("send1");echo "[main]", $ret, "\n";$ret = $gen->throw(new Exception("Test"));echo "[main]", $ret, "\n";/* * [main]yield1 * [gen]send1 * [main]yield2 * [gen][Exception]Test * [gen]finish * [main] */

    圖 4 Coroutine錯誤處理樣本

    實戰演習

    前面簡單介紹了相關的語言設施,那麼具體到實際項目中,到底應該如何運用呢?讓我們繼續《一次失敗的PHP擴充開發之旅》描述的情境,藉助上述特性實現那個美好的願望:以同步方式書寫非同步代碼!

    第一版初稿

    handler = $handler;        $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);        if(!$this->socket) {            die(socket_strerror(socket_last_error())."\n");        }        if (!socket_set_nonblock($this->socket)) {            die(socket_strerror(socket_last_error())."\n");        }        if(!socket_bind($this->socket, "0.0.0.0", 1234)) {            die(socket_strerror(socket_last_error())."\n");        }    }    public function Run() {        while (true) {            $reads = array($this->socket);            foreach ($this->tasks as list($socket)) {                $reads[] = $socket;            }            $writes = NULL;            $excepts= NULL;            if (!socket_select($reads, $writes, $excepts, 0, 1000)) {                continue;            }            foreach ($reads as $one) {                $len = socket_recvfrom($one, $data, 65535, 0, $ip, $port);                if (!$len) {                    //echo "socket_recvfrom fail.\n";                    continue;                }                if ($one == $this->socket) {                    //echo "[Run]request recvfrom succ. data=$data ip=$ip port=$port\n";                    $handler = $this->handler;                    $coroutine = $handler($one, $data, $len, $ip, $port);                    $task = $coroutine->current();                    //echo "[Run]AsyncTask recv. data=$task->data ip=$task->ip port=$task->port timeout=$task->timeout\n";                    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);                    if(!$socket) {                        //echo socket_strerror(socket_last_error())."\n";                        $coroutine->throw(new Exception(socket_strerror(socket_last_error()), socket_last_error()));                        continue;                    }                    if (!socket_set_nonblock($socket)) {                        //echo socket_strerror(socket_last_error())."\n";                        $coroutine->throw(new Exception(socket_strerror(socket_last_error()), socket_last_error()));                        continue;                    }                    socket_sendto($socket, $task->data, $task->len, 0, $task->ip, $task->port);                    $this->tasks[$socket] = [$socket, $coroutine];                } else {                    //echo "[Run]response recvfrom succ. data=$data ip=$ip port=$port\n";                    if (!isset($this->tasks[$one])) {                        //echo "no async_task found.\n";                    } else {                        list($socket, $coroutine) = $this->tasks[$one];                        unset($this->tasks[$one]);                        socket_close($socket);                        $coroutine->send(array($data, $len));                    }                }            }        }    }}class AsyncTask {    public $data;    public $len;    public $ip;    public $port;    public $timeout;    public function __construct($data, $len, $ip, $port, $timeout) {        $this->data = $data;        $this->len = $len;        $this->ip = $ip;        $this->port = $port;        $this->timeout = $timeout;    }}function RequestHandler($socket, $req_buf, $req_len, $ip, $port) {    //echo "[RequestHandler] before yield AsyncTask. REQ=$req_buf\n";    list($rsp_buf, $rsp_len) = (yield new AsyncTask($req_buf, $req_len, "127.0.0.1", 2345, 1000));    //echo "[RequestHandler] after yield AsyncTask. RSP=$rsp_buf\n";    socket_sendto($socket, $rsp_buf, $rsp_len, 0, $ip, $port);}$server = new AsyncServer(RequestHandler);$server->Run();?>

    代碼解讀:

  • 為了便於說明問題,這裡所有底層通訊基於UDP,省略了TCP的connect等繁瑣細節
  • AsyncServer為底層架構類,封裝了網路通訊細節以及協程切換細節,通過socket進行coroutine綁定
  • RequestHandler為業務處理函數,通過yield new AsyncTask()實現非同步網路互動
  • 第二版完善

    第一版遺留問題:

  • 非同步網路互動的timeout未實現,僅預留了介面參數
  • yield new AsyncTask()調用方式不夠自然,略感彆扭
  • handler = $handler;        $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);        if(!$this->socket) {            die(socket_strerror(socket_last_error())."\n");        }        if (!socket_set_nonblock($this->socket)) {            die(socket_strerror(socket_last_error())."\n");        }        if(!socket_bind($this->socket, "0.0.0.0", 1234)) {            die(socket_strerror(socket_last_error())."\n");        }    }    public function Run() {        while (true) {            $now = microtime(true) * 1000;            foreach ($this->timers as $time => $sockets) {                if ($time > $now) break;                foreach ($sockets as $one) {                    list($socket, $coroutine) = $this->tasks[$one];                    unset($this->tasks[$one]);                    socket_close($socket);                    $coroutine->throw(new Exception("Timeout"));                }                unset($this->timers[$time]);            }            $reads = array($this->socket);            foreach ($this->tasks as list($socket)) {                $reads[] = $socket;            }            $writes = NULL;            $excepts= NULL;            if (!socket_select($reads, $writes, $excepts, 0, 1000)) {                continue;            }            foreach ($reads as $one) {                $len = socket_recvfrom($one, $data, 65535, 0, $ip, $port);                if (!$len) {                    //echo "socket_recvfrom fail.\n";                    continue;                }                if ($one == $this->socket) {                    //echo "[Run]request recvfrom succ. data=$data ip=$ip port=$port\n";                    $handler = $this->handler;                    $coroutine = $handler($one, $data, $len, $ip, $port);                    if (!$coroutine) {                        //echo "[Run]everything is done.\n";                        continue;                    }                    $task = $coroutine->current();                    //echo "[Run]AsyncTask recv. data=$task->data ip=$task->ip port=$task->port timeout=$task->timeout\n";                    $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);                    if(!$socket) {                        //echo socket_strerror(socket_last_error())."\n";                        $coroutine->throw(new Exception(socket_strerror(socket_last_error()), socket_last_error()));                        continue;                    }                    if (!socket_set_nonblock($socket)) {                        //echo socket_strerror(socket_last_error())."\n";                        $coroutine->throw(new Exception(socket_strerror(socket_last_error()), socket_last_error()));                        continue;                    }                    socket_sendto($socket, $task->data, $task->len, 0, $task->ip, $task->port);                    $deadline = $now + $task->timeout;                    $this->tasks[$socket] = [$socket, $coroutine, $deadline];                    $this->timers[$deadline][$socket] = $socket;                } else {                    //echo "[Run]response recvfrom succ. data=$data ip=$ip port=$port\n";                    list($socket, $coroutine, $deadline) = $this->tasks[$one];                    unset($this->tasks[$one]);                    unset($this->timers[$deadline][$one]);                    socket_close($socket);                    $coroutine->send(array($data, $len));                }            }        }    }}class AsyncTask {    public $data;    public $len;    public $ip;    public $port;    public $timeout;    public function __construct($data, $len, $ip, $port, $timeout) {        $this->data = $data;        $this->len = $len;        $this->ip = $ip;        $this->port = $port;        $this->timeout = $timeout;    }}function AsyncSendRecv($req_buf, $req_len, $ip, $port, $timeout) {    return new AsyncTask($req_buf, $req_len, $ip, $port, $timeout);}function RequestHandler($socket, $req_buf, $req_len, $ip, $port) {    //echo "[RequestHandler] before yield AsyncTask. REQ=$req_buf\n";    try {        list($rsp_buf, $rsp_len) = (yield AsyncSendRecv($req_buf, $req_len, "127.0.0.1", 2345, 3000));    } catch (Exception $ex) {        $rsp_buf = $ex->getMessage();        $rsp_len = strlen($rsp_buf);        //echo "[Exception]$rsp_buf\n";    }    //echo "[RequestHandler] after yield AsyncTask. RSP=$rsp_buf\n";    socket_sendto($socket, $rsp_buf, $rsp_len, 0, $ip, $port);}$server = new AsyncServer(RequestHandler);$server->Run();?>

    代碼解讀:

  • 藉助PHP內建array能力,實現簡單的“逾時管理”,以毫秒為精度作為時間分區
  • 封裝AsyncSendRecv介面,調用形如yield AsyncSendRecv(),更加自然
  • 添加Exception作為錯誤處理機制,添加ret_code亦可,僅為展示之用
  • 效能測試

    測試環境


    測試資料

    100Byte/REQ 1000Byte/REQ
    async_svr_v1.php 16000/s 15000/s
    async_svr_v2.php 11000/s 10000/s
    展望未來
  • 有興趣的PHPer可以基於該思路進行底層架構封裝,對於常見阻塞操作進行封裝,比如:connect、send、recv、sleep ...
  • 本人接觸PHP時日尚淺,很多用法非最優,高手可有針對性最佳化,效能應該可以繼續提高
  • 目前基於socket進行coroutine綁定,如果基於TCP通訊,每次connect/close,開銷過大,需要考慮實現串連池
  • python等語言也有類似的語言設施,有興趣的讀者可以自行研究
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.