<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 在多個協程間共用同一個協程客戶端 [TOC] 與同步阻塞程序不同,協程是并發處理請求的,因此同一時間可能會有很多個請求在并行處理,一旦共用客戶端連接,就會導致不同協程之間發生數據錯亂。 #### ?錯誤的代碼 ~~~ $server = new Swoole\Http\Server('127.0.0.1', 9501); $server->on('Receive', function ($serv, $fd, $rid, $data) { $redis = RedisFactory::getRedis(); $result = $redis->hgetall('key'); $resp->end(var_export($result, true)); }); $server->start(); class RedisFactory { private static $_redis = null; public static function getRedis() { if (null === self::$_redis) { $redis = new \Swoole\Coroutine\Redis(); $redis->connect('127.0.0.1', 6379); self::$_redis = $redis; } return self::$_redis; } } ~~~ #### ?正確的代碼 基于`SplQueue`實現協程客戶端的連接池,可以復用協程客戶端,實現長連接。 ~~~ $pool = new RedisPool(); $server = new Swoole\Http\Server('127.0.0.1', 9501); $server->set([ // 如開啟異步安全重啟, 需要在workerExit釋放連接池資源 'reload_async' => true ]); $server->on('start', function (swoole_http_server $server) { var_dump($server->master_pid); }); $server->on('workerExit', function (swoole_http_server $server) use ($pool) { $pool->destruct(); }); $server->on('request', function (swoole_http_request $req, swoole_http_response $resp) use ($pool) { //從連接池中獲取一個Redis協程客戶端 $redis = $pool->get(); //連接失敗 if ($redis === false) { $resp->end("ERROR"); return; } $result = $redis->hgetall('key'); $resp->end(var_export($result, true)); //釋放客戶端,其他協程可復用此對象 $pool->put($redis); }); $server->start(); class RedisPool { protected $available = true; protected $pool; public function __construct() { $this->pool = new SplQueue; } public function put($redis) { $this->pool->push($redis); } /** * @return bool|mixed|\Swoole\Coroutine\Redis */ public function get() { //有空閑連接且連接池處于可用狀態 if ($this->available && count($this->pool) > 0) { return $this->pool->pop(); } //無空閑連接,創建新連接 $redis = new Swoole\Coroutine\Redis(); $res = $redis->connect('127.0.0.1', 6379); if ($res == false) { return false; } else { return $redis; } } public function destruct() { // 連接池銷毀, 置不可用狀態, 防止新的客戶端進入常駐連接池, 導致服務器無法平滑退出 $this->available = false; while (!$this->pool->isEmpty()) { $this->pool->pop(); } } } ~~~
                  <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>

                              哎呀哎呀视频在线观看