PHP非阻塞模式

來源:互聯網
上載者:User

標籤:

PHP非阻塞模式

by 塵緣 on 七月 31st, 2014 // Filed Under → php

讓PHP不再阻塞當PHP作為後端處理需要完成一些長時間處理,為了快速響應頁面請求,不作結果返回判斷的情況下,可以有如下措施:

一、若你使用的是FastCGI模式,使用fastcgi_finish_request()能馬上結束會話,但PHP線程繼續在跑。

協助
01 02 03 04 05 06 07 08 09 10 echo"program start.";   file_put_contents(‘log.txt‘,‘start-time:‘.date(‘Y-m-d H:i:s‘), FILE_APPEND); fastcgi_finish_request(); sleep(1); echo‘debug...‘; file_put_contents(‘log.txt‘,‘start-proceed:‘.date(‘Y-m-d H:i:s‘), FILE_APPEND);   sleep(10); file_put_contents(‘log.txt‘,‘end-time:‘.date(‘Y-m-d H:i:s‘), FILE_APPEND);

這個例子輸出結果可看到輸出program start.後會話就返回了,所以debug那個輸出瀏覽器是接收不到的,而log.txt檔案能完整接收到三個完成時間。

二、使用fsockopen、cUrl的非阻塞模式請求另外的網址

協助
1 2 3 4 5 6 7 8 $fp=fsockopen("www.example.com", 80,$errno,$errstr, 30); if(!$fp)die(‘error fsockopen‘); stream_set_blocking($fp,0); $http="GET /save.php  / HTTP/1.1\r\n";    $http.="Host: www.example.com\r\n";    $http.="Connection: Close\r\n\r\n"; fwrite($fp,$http); fclose($fp);

利用cURL中的curl_multi_*函數發送非同步請求

協助
1 2 3 4 5 6 $cmh= curl_multi_init(); $ch1= curl_init(); curl_setopt($ch1, CURLOPT_URL,"http://localhost:6666/child.php"); curl_multi_add_handle($cmh,$ch1); curl_multi_exec($cmh,$active); echo"End\n";

三、使用Gearman、Swoole擴充
Gearman是一個具有php擴充的分布式非同步處理架構,能處理大批量非同步任務;
Swoole最近很火,有很多非同步方法呼叫,使用簡單。(塵緣註:號稱重新定義PHP,把NodeJS噴得體無完膚。Swoole工具雖好,卻感覺是擴充本身跟NodeJS沒可比性)

四、使用redis等緩衝、隊列,將資料寫入緩衝,使用後台計劃任務實現資料非同步處理。
這個方法在常見的大流量架構中應該很常見吧

五、極端的情況下,可以調用系統命令,可以將資料傳給背景工作執行,個人感覺不是很高效。

協助
1 2 $cmd=‘nohup php ./processd.php $someVar >/dev/null  &‘; `$cmd`

六、外國佬的大招,沒看懂,php原生支援

http://nikic.github.io/2012/12/22/Cooperative-multitasking-using-coroutines-in-PHP.html

七、安裝pcntl擴充,使用pcntl_fork產生子進程非同步執行任務,個人覺得是最方便的,但也容易出現zombie process。

協助
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 if(($pid= pcntl_fork()) == 0) {     child_func();   //子進程函數,主進程運行 }else{     father_func();  //主進程函數 }   echo"Process ".getmypid() ." get to the end.\n";   functionfather_func() {     echo"Father pid is ".getmypid() ."\n"; }   functionchild_func() {     sleep(6);     echo"Child process exit pid is ".getmypid() ."\n";     exit(0); }

PHP非阻塞模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.