<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 使用協程的優勢 Swoole是高性能異步框架,正因為異步所以高性能,但是異步也有異步的不好,寫邏輯代碼有時候就非常的不方便,需要多層嵌套回調,弄得代碼的可讀性很差維護起來非常的不方便,那么如何解決這個弊端呢?那就是使用協程。 SwooleDistributed框架提供了一套基于yield關鍵字的協程寫法。 #回調風格和協程風格的區別 舉例說明: 回調風格: ```php /** * mysql 測試 * @throws \Server\CoreBase\SwooleException */ public function mysql_test() { $this->mysql_pool->dbQueryBuilder->select('*')->from('account')->where('sex', 1); $this->mysql_pool->query(function ($result) { print_r($result); $this->destroy(); }); } ``` 協程風格: ```php /** * mysql 測試 * @throws \Server\CoreBase\SwooleException */ public function mysql_test() { $mySqlCoroutine = $this->mysql_pool->dbQueryBuilder->select('*')->from('account')->where('sex', 1)->coroutineSend(); $result = yield $mySqlCoroutine; print_r($result); $this->destroy(); } ``` 上述代碼還只是基礎,想想看多次請求并且依賴的情況 回調風格: ```php public function test($callback) { $this->redis_pool->get('test',function ($uid)use($callback){ $this->mysql_pool->dbQueryBuilder->select('*')->from('account')->where('uid', $uid); $this->mysql_pool->query(function ($result)use($callback) { call_user_func($callback,$result); }); }); } ``` 協程風格: ```php public function test() { $redisCoroutine = $this->redis_pool->getCoroutine()->get('test'); $uid = yield $redisCoroutine; $mySqlCoroutine = $this->mysql_pool->dbQueryBuilder->select('*')->from('account')->where('uid', $uid)->coroutineSend(); $result = yield $mySqlCoroutine; return $result; } ``` 上面的代碼是在一個model中,按照以往的回調風格,controller調用這個model還需要傳進去一個回調函數才能獲取到值(我覺得這么做的人一定會瘋),而協程風格你只需要按部就班的return結果就行了。和同步的代碼唯一的區別就是多了一個yield關鍵字。 好了我們再看調用這個model的controller怎么寫。 回調風格: ```php /** * 非協程測試 */ public function http_testNOCoroutine() { $this->testModel = $this->loader->model('TestModel', $this); $this->testModel->test(function($result){ $this->http_output->end($result); }); } ``` 協程風格: ```php /** * 協程測試 */ public function http_testCoroutine() { $this->testModel = $this->loader->model('TestModel', $this); $result = yield $this->testModel->test(); $this->http_output->end($result); } ``` 同樣只是要多加一個yield關鍵字。是不是很方便~,注意只有這個model的方法內使用了yield,控制器調用的時候才需要加yield關鍵字。普通的方法是不需要的,當然你加了也不會報錯。 # 協程中的異常? swooleDistributed框架已經將協程的使用變得很方便了,至于異常和平常的寫法一毛一樣。 ```php public function test_exceptionII() { $mySqlCoroutine = $this->mysql_pool->dbQueryBuilder->select('*')->from('account')->where('uid', 10303)->coroutineSend(); $result = yield $mySqlCoroutine; throw new \Exception('test'); } ``` 無論你是在model中還是在controller中,還是在model的model中。。。。 總之throw出來就行,上一層使用try可以捕獲到錯誤。 這里還有個小小的體驗優化,當報錯一直到controller層的時候,會被controller的onExceptionHandle捕獲到,你可以重寫這個函數實現異常情況下的客戶端回復。 # 協程的嵌套 支持無限級的嵌套。 # 協程的調度順序 調節yield的位置即可調節接收的順序。 ```php $redisCoroutine = $this->redis_pool->getCoroutine()->get('test'); $uid = yield $redisCoroutine; $mySqlCoroutine = $this->mysql_pool->dbQueryBuilder->select('*')->from('account')->where('uid', $uid)->coroutineSend(); $result = yield $mySqlCoroutine; ``` 上面的代碼調度順序是redis_send->redis_rev->mysql_send->mysql_rev。 ```php $redisCoroutine = $this->redis_pool->getCoroutine()->get('test'); $mySqlCoroutine = $this->mysql_pool->dbQueryBuilder->select('*')->from('account')->where('uid', 123)->coroutineSend(); $uid = yield $redisCoroutine; $result = yield $mySqlCoroutine; ``` 上面的代碼調度順序是redis_send->mysql_send->redis_rev->mysql_rev。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看