久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放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)站

      詳細(xì)介紹php五大io模型之阻塞與非阻塞

      本篇文章給大家詳細(xì)介紹php五大io模型之阻塞與非阻塞。有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對大家有所幫助。

      詳細(xì)介紹php五大io模型之阻塞與非阻塞

      php實(shí)現(xiàn)五大io模型-1阻塞與非阻塞

      阻塞:是指應(yīng)用程序執(zhí)行IO操作需要徹底完成后才返回到用戶空間

      非阻塞:是指應(yīng)用程序執(zhí)行IO操作被調(diào)用后立即返回給用戶一個狀態(tài)值,無需等到IO操作徹底完成。

      阻塞模型:

      詳細(xì)介紹php五大io模型之阻塞與非阻塞

      非阻塞模式:

      詳細(xì)介紹php五大io模型之阻塞與非阻塞

      阻塞模式下,用戶進(jìn)程會一直等待內(nèi)核態(tài)數(shù)據(jù),所以效率極低。打個比方:小明要燒水,他就一直等著水燒開才行,但其實(shí),在燒水過程中小明還能去做別事。

      做個優(yōu)化就是非阻塞模式,用戶進(jìn)程在執(zhí)行IO操作后,內(nèi)核態(tài)會立即返回一個數(shù)值通常就是空,用戶進(jìn)程可以去做別的事,等內(nèi)核態(tài)數(shù)據(jù)結(jié)果好了在請求獲取真正的執(zhí)行結(jié)果。以上面小明例子就是,小明燒水時候,就可以去看書了,看一會去看看水是否開了,檢查水是否燒開,同時又能看書學(xué)習(xí)。

      性能分析:

      假設(shè)一個程序需要執(zhí)行兩個操作a和b,其中a需要執(zhí)行IO操作,b不需要。阻塞模式下消耗時間等于a+b,而非阻塞模式等于a和b中消耗時間最大都操作。

      服務(wù)端代碼:

      $server = stream_socket_server('tcp://127.0.0.1:9999', $erron, $error); while (true) {  $conn = stream_socket_accept($server);  if ($conn) {  $data = fread($conn, 65535);  echo $data;  sleep(3);  fwrite($conn, 'this is server');  fclose($conn);  } }

      阻塞客戶端:

      function operation() {  sleep(2); } echo "client start n"; $client =  stream_socket_client('tcp://127.0.0.1:9999', $erron, $error, 60); fwrite($client, "is client 1n"); echo fread($client, 65535); operation(); fclose($client);

      非阻塞客戶端:

      function operation() {  sleep(2); } echo "client start n"; $client =  stream_socket_client('tcp://127.0.0.1:9999', $erron, $error, 60); stream_set_blocking($client, 0);//設(shè)置非阻塞 fwrite($client, "is client 1n"); operation(); while (!feof($client)) {  sleep(1);  var_dump(fread($client, 65535)); } fclose($client);

      時間消耗:

      阻塞模式

      詳細(xì)介紹php五大io模型之阻塞與非阻塞

      非阻塞模式

      詳細(xì)介紹php五大io模型之阻塞與非阻塞

      推薦學(xué)習(xí):php視頻教程

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