久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      嘗試怎么將Laravel改成Swoole版

      本篇文章給大家Laravel和Swoole的相關(guān)知識,主要內(nèi)容是教大家怎么將Laravel改成Swoole版(嘗試學(xué)習(xí),不建議更改現(xiàn)有項(xiàng)目),下面一起來看看吧,希望對大家有所幫助!

      前言

      不建議生產(chǎn)環(huán)境使用

      創(chuàng)建一個新的 laravel 項(xiàng)目

      laravel new swoole-laravel
      登錄后復(fù)制

      將 Laravel 改成 Swoole 版

      Laravel 的根目錄創(chuàng)建一個 swoole_server.php 文件,然后把 public/index.php 中的代碼復(fù)制過來【推薦學(xué)習(xí):laravel視頻教程】

      <?php use IlluminateContractsHttpKernel; use IlluminateHttpRequest; define('LARAVEL_START', microtime(true)); require __DIR__.'/../vendor/autoload.php'; $app = require_once __DIR__.'/../bootstrap/app.php'; $kernel = $app->make(Kernel::class); $response = $kernel->handle(     $request = Request::capture() )->send(); $kernel->terminate($request, $response);
      登錄后復(fù)制

      第一步,框架文件的加載是肯定的,而且應(yīng)該是在主進(jìn)程中就加載好的,不需要子進(jìn)程或者協(xié)程再去重復(fù)加載。因此,上面的 require 都不太需要動。

      第二步,我們要啟動一個 HTTP 的 Swoole 服務(wù),這個之前已經(jīng)講過很多次了,注意,在 onRequest 中,我們應(yīng)該將 $kernel 相關(guān)的代碼放入進(jìn)去。

      $http = new SwooleHttpServer('0.0.0.0', 9501); $http->on('Request', function ($req, $res) use($app) {     try {         $kernel = $app->make(Kernel::class);         $response = $kernel->handle(             $request = Request::capture()         )->send();         $kernel->terminate($request, $response);     }catch(Exception $e){         print_r($e->getMessage());     } }); echo "服務(wù)啟動", PHP_EOL; $http->start();
      登錄后復(fù)制

      這樣就可以了嗎?要不你先試試看。正常情況下可能你是獲得不了任何的輸入和輸出的,這是為啥?

      第三步,解決輸入問題,其實(shí)就是超全局變量在 Swoole 中是不起作用的,所以 $_GET 之類的變量都會失效,Laravel 中 Request 相關(guān)的對象都無法獲得數(shù)據(jù)了。這怎么辦呢?我們從 onRequest 的參數(shù)中拿這些數(shù)據(jù),然后再放回到當(dāng)前進(jìn)程協(xié)程中的 $_GET 中就好啦。

      $http->on('Request', function ($req, $res) use($app) {     $_SERVER = [];     if(isset($req->server)){         foreach($req->server as $k => $v){             $_SERVER[strtoupper($k)] = $v;         }     }     $_GET = [];     if(isset($req->get)){         foreach ($req->get as $k => $v){             $_GET[$k] = $v;         }     }     $_POST = [];     if(isset($req->post)){         foreach ($req->post as $k => $v){             $_POST[$k] = $v;         }     }     try {         $kernel = $app->make(Kernel::class);         $response = $kernel->handle(             $request = Request::capture()         )->send();         $kernel->terminate($request, $response);     }catch(Exception $e){         print_r($e->getMessage());     } });
      登錄后復(fù)制

      上面三段代碼,分別解決了 $_SERVER、$_GET 和 $_POST 的問題。現(xiàn)在你再試試,參數(shù)是可以接收到了,但輸出怎么是打印在控制臺的?

      第四步,解決輸出問題,將框架中的所有輸出放到輸出緩沖區(qū),然后再用 Swoole 的 Response 返回。

      $http->on('Request', function ($req, $res) use($app) {     $_SERVER = [];     if(isset($req->server)){         foreach($req->server as $k => $v){             $_SERVER[strtoupper($k)] = $v;         }     }     $_GET = [];     if(isset($req->get)){         foreach ($req->get as $k => $v){             $_GET[$k] = $v;         }     }     $_POST = [];     if(isset($req->post)){         foreach ($req->post as $k => $v){             $_POST[$k] = $v;         }     }     //把返回放到一個緩沖區(qū)里     ob_start();     try {         $kernel = $app->make(Kernel::class);         $response = $kernel->handle(             $request = Request::capture()         )->send();         $kernel->terminate($request, $response);     }catch(Exception $e){         print_r($e->getMessage());     }     $ob = ob_get_contents();     ob_end_clean();     $res->end($ob); });
      登錄后復(fù)制

      最后的 ob_start () 這些內(nèi)容,也是我們之前學(xué)習(xí)過的內(nèi)容,也就不多做解釋了。

      全部代碼

      <?php use IlluminateContractsHttpKernel; use IlluminateHttpRequest; define('LARAVEL_START', microtime(true)); require __DIR__.'/vendor/autoload.php'; $app = require_once __DIR__.'/bootstrap/app.php'; $http = new SwooleHttpServer('0.0.0.0', 9501); $http->on('Request', function ($req, $res) use($app) {     $_SERVER = [];     if(isset($req->server)){         foreach($req->server as $k => $v){             $_SERVER[strtoupper($k)] = $v;         }     }     $_GET = [];     if(isset($req->get)){         foreach ($req->get as $k => $v){             $_GET[$k] = $v;         }     }     $_POST = [];     if(isset($req->post)){         foreach ($req->post as $k => $v){             $_POST[$k] = $v;         }     }     //把返回放到一個緩沖區(qū)里     ob_start();     try {         $kernel = $app->make(Kernel::class);         $response = $kernel->handle(             $request = Request::capture()         )->send();         $kernel->terminate($request, $response);     }catch(Exception $e){         print_r($e->getMessage());     }     $ob = ob_get_contents();     ob_end_clean();     $res->end($ob); }); echo "服務(wù)啟動", PHP_EOL; $http->start();
      登錄后復(fù)制

      至此,我們最簡單的框架改造就完成了,趕緊試試效果吧。

      運(yùn)行

      php swoole_server.php
      登錄后復(fù)制

      訪問

      http://47.113.xxx.xx:9501/
      登錄后復(fù)制

      試試協(xié)程效果

      先定義一個路由?;蛘呶覀冎苯痈脑煲幌履J(rèn)的路由。

      Route::get('/', function () {     echo SwooleCoroutine::getCid(), "<br/>";     print_r(SwooleCoroutine::stats());     SwooleCoroutine::sleep(10);     echo "<br/>";     echo getmypid(), "<br/>"; //    return view('welcome'); });
      登錄后復(fù)制

      打印了一堆東西,不過應(yīng)該都比較熟悉吧,前兩個是協(xié)程 ID 和協(xié)程信息的輸出,然后我們 SwooleCoroutine::sleep () 了 10 秒,再打印一下進(jìn)程 ID 。

      然后我們打開瀏覽器,準(zhǔn)備兩個標(biāo)簽一起訪問。

      // 第一個訪問的頁面 1 Array (     [event_num] => 2     [signal_listener_num] => 0     [aio_task_num] => 0     [aio_worker_num] => 0     [aio_queue_size] => 0     [c_stack_size] => 2097152     [coroutine_num] => 1     [coroutine_peak_num] => 1     [coroutine_last_cid] => 1 ) 1468 // 第二個訪問的頁面 2 Array (     [event_num] => 2     [signal_listener_num] => 0     [aio_task_num] => 0     [aio_worker_num] => 0     [aio_queue_size] => 0     [c_stack_size] => 2097152     [coroutine_num] => 2     [coroutine_peak_num] => 2     [coroutine_last_cid] => 2 ) 1468
      登錄后復(fù)制

      看出來了嗎?每個 onRequest 事件其實(shí)都是開了一個新的協(xié)程來處理請求所以它們的協(xié)程 ID 不同。同時,第二個請求不會因?yàn)榈谝粋€請求阻塞而等到 20 秒后才返回。最后在協(xié)程狀態(tài)中,我們還看到了第二個請求中顯示 coroutine_num 有兩個,說明當(dāng)前有兩個協(xié)程在處理任務(wù)。最后,進(jìn)程是相同的,它們都是走的同一個進(jìn)程。

      試試多進(jìn)程效果

      默認(rèn)情況下,上面的代碼是一個主進(jìn)程,一個 Worker 進(jìn)程,然后再使用了協(xié)程能力。其實(shí)這樣的效果已經(jīng)能秒殺普通的 PHP-FPM 效果了。但我們要充分利用多核機(jī)器的性能,也就是說,我們來開啟多進(jìn)程,使用多進(jìn)程 + 多協(xié)程的超強(qiáng)處理模式。最簡單的方式,直接設(shè)置 HTTP 服務(wù)的進(jìn)程 Worker 數(shù)量即可。

      $http->set(array(     'worker_num' => 4,       // 'worker_num' => 1,單進(jìn)程 ));
      登錄后復(fù)制

      現(xiàn)在運(yùn)行起服務(wù)器,可以看到多了幾個進(jìn)程了。然后我們再新建一個測試路由

      Route::get('/a', function () {     echo SwooleCoroutine::getCid(), "<br/>";     print_r(SwooleCoroutine::stats());     echo "<br/>";     echo getmypid(), "<br/>"; });
      登錄后復(fù)制

      現(xiàn)在再次訪問首頁和這個 /a 頁面。

      // 首頁一 1 Array (     [event_num] => 2     [signal_listener_num] => 0     [aio_task_num] => 0     [aio_worker_num] => 0     [aio_queue_size] => 0     [c_stack_size] => 2097152     [coroutine_num] => 1     [coroutine_peak_num] => 1     [coroutine_last_cid] => 1 ) 1562 // 首頁二 1 Array (     [event_num] => 2     [signal_listener_num] => 0     [aio_task_num] => 0     [aio_worker_num] => 0     [aio_queue_size] => 0     [c_stack_size] => 2097152     [coroutine_num] => 1     [coroutine_peak_num] => 1     [coroutine_last_cid] => 1 ) 1563 // /a 頁面 1 Array (     [event_num] => 2     [signal_listener_num] => 0     [aio_task_num] => 0     [aio_worker_num] => 0     [aio_queue_size] => 0     [c_stack_size] => 2097152     [coroutine_num] => 1     [coroutine_peak_num] => 1     [coroutine_last_cid] => 1 ) 1564
      登錄后復(fù)制

      發(fā)現(xiàn)沒有,它們的進(jìn)程 ID 也都不同了吧,如果沒有阻塞,會優(yōu)先切換進(jìn)程,如果所有進(jìn)程都有阻塞,則再循環(huán)創(chuàng)建協(xié)程進(jìn)行進(jìn)程內(nèi)的處理。

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號