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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 存在問題 見下篇 `協程上下文解決request` 問題。 ## swoole [swoole文檔](https://wiki.swoole.com/#/start/start_http_server) 本篇講解使用協程 `http` 方式,參考了下[hyperf/src/server/src/Server.php](https://github.com/hyperf/hyperf/blob/master/src/server/src/Server.php)。 ## 使用函數 不使用 [服務器端 (協程風格) HTTP 服務器](https://wiki.swoole.com/#/coroutine/http_server) 使用 [服務器端 (異步風格) HTTP 服務器](https://wiki.swoole.com/#/http_server) 因為[服務器端 (協程風格) HTTP 服務器](https://wiki.swoole.com/#/coroutine/http_server)設置[pid_file](https://wiki.swoole.com/#/server/setting?id=pid_file)是沒用的。 用`異步風格` 也可以用協程: [enable_coroutine](https://wiki.swoole.com/#/server/setting?id=enable_coroutine) ## 創建swoole.php ``` <?php use core\request\PhpRequest; error_reporting(E_ALL); ini_set("display_errors","On"); require_once __DIR__ . '/vendor/autoload.php'; // 引入自動加載 require_once __DIR__ . '/app.php'; // 框架的文件 $start = function() { Swoole\Runtime::enableCoroutine($flags = SWOOLE_HOOK_ALL); // 如果你用了CURL PDO之類的客戶端 請開啟這個客戶端協程化 // 詳細見文檔: https://wiki.swoole.com/#/runtime // 綁定主機 端口 $http = new Swoole\Http\Server('0.0.0.0', 9501); $http->set([ // 進程pid文件 'pid_file' => FRAME_BASE_PATH.'/storage/swoole.pid', 'enable_coroutine' => true, // 開啟異步協程化 默認開啟 'worker_num' => 4 // Worker進程數 跟協程處理有關系 https://wiki.swoole.com/#/server/setting?id=worker_num ]); $http->on('request', function ($request, $response) { echo Swoole\Coroutine::getCid().PHP_EOL; // 打印當前協程id // 文檔:https://wiki.swoole.com/#/coroutine/coroutine?id=getcid // 綁定request 現在這個是有問題 因為容器的單例的 所以request會一直變 (數據錯亂) // 應該使用協程上下文來保存request 下一篇文章會講 $server = $request->server; app()->bind(\core\request\RequestInterface::class,function () use ($server){ return PhpRequest::create( $server['path_info'], $server['request_method'], $server ); },false); $response->end( app('response')->setContent( // 響應 app('router')->dispatch( // 路由 app(\core\request\RequestInterface::class) // 請求 ) )->getContent() ); }); echo 'start ok'.PHP_EOL; $http->start(); }; $stop = function () { if(! file_exists(FRAME_BASE_PATH.'/storage/swoole.pid')) return; $pid = file_get_contents(FRAME_BASE_PATH.'/storage/swoole.pid'); Swoole\Process::kill($pid); // 見文檔 }; $handle = $argv[1]; // 啟動 if( $handle == 'start') $start(); // 停止 elseif( $handle == 'stop'); $stop(); ``` ## 運行 ``` php swoole.php start ``` ![](https://img.kancloud.cn/1e/bd/1ebd5558d64efe5e0cc40382446b9e4f_1068x472.png) ![](https://img.kancloud.cn/1f/28/1f285c7ab52c9726c910c7c0f506fb0b_925x412.png) ![](https://img.kancloud.cn/a1/fa/a1fa0f54c078424f91c702aa695e3f58_860x264.png) ## 關于協程id重復解釋 ### 使用ab測試 ![](https://img.kancloud.cn/c4/8a/c48a718509d1e6780c29aea257800714_718x90.png) ``` ab -n 8 -c 8 http://127.0.0.1:9501/ ``` ### 結果 ![](https://img.kancloud.cn/9c/5e/9c5e66c640732321dff27c413fcd1265_723x234.png) `echo Swoole\Coroutine::getCid().PHP_EOL; // 打印當前協程id ` 這里打印了 `協程id`, 你會發現 `協程id` 重復了。 不用擔心, `協程id` 這是唯一的,。 是因為設置了 `'worker_num' => 4`。
                  <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>

                              哎呀哎呀视频在线观看