PHP多進程實現

來源:互聯網
上載者:User

標籤:company   fork   exe   data   執行   highlight   系統   數組   enable   

PHP多進程實現

php有一組進程式控制制函數(編譯時間需要–enable-pcntl與posix擴充),使得php能在nginx系統中實現跟c一樣的建立子進程、使用exec函數執行程式、處理訊號等功能。

CentOS 6 下yum安裝php的,預設是不安裝pcntl的,因此需要單獨編譯安裝,首先下載對應版本的php,解壓後

[plain] view plain copy print?
  1. cd php-version/ext/pcntl  
  2. phpize  
  3. ./configure && make && make install  
  4. cp /usr/lib/php/modules/pcntl.so /usr/lib64/php/modules/pcntl.so  
  5. echo "extension=pcntl.so" >> /etc/php.ini  
  6. /etc/init.d/httpd restart  

方便極了。

下面是範例程式碼:

[php] view plain copy print?
    1. <?php  
    2. header(‘content-type:text/html;charset=utf-8‘ );  
    3.   
    4. // 必須載入擴充  
    5. if (!function_exists("pcntl_fork")) {  
    6.     die("pcntl extention is must !");  
    7. }  
    8. //總進程的數量  
    9. $totals = 3;  
    10. // 執行的指令碼數量  
    11. $cmdArr = array();  
    12. // 執行的指令碼數量的數組  
    13. for ($i = 0; $i < $totals; $i++) {  
    14.     $cmdArr[] = array("path" => __DIR__ . "/run.php",  ‘pid‘ =>$i ,‘total‘ =>$totals);  
    15. }  
    16.   
    17. /* 
    18. 展開:$cmdArr 
    19. Array 
    20.     [0] => Array 
    21.         ( 
    22.             [path] => /var/www/html/company/pcntl/run.php 
    23.             [pid] => 0 
    24.             [total] => 3 
    25.         ) 
    26.  
    27.     [1] => Array 
    28.         ( 
    29.             [path] => /var/www/html/company/pcntl/run.php 
    30.             [pid] => 1 
    31.             [total] => 3 
    32.         ) 
    33.  
    34.     [2] => Array 
    35.         ( 
    36.             [path] => /var/www/html/company/pcntl/run.php 
    37.             [pid] => 2 
    38.             [total] => 3 
    39.         ) 
    40.  
    41. */  
    42.   
    43. pcntl_signal(SIGCHLD, SIG_IGN); //如果父進程不關心子進程什麼時候結束,子進程結束後,核心會回收。  
    44. foreach ($cmdArr  as   $cmd) {  
    45.     $pid = pcntl_fork();    //建立子進程  
    46.     //父進程和子進程都會執行下面代碼  
    47.     if ($pid == -1) {  
    48.         //錯誤處理:建立子進程失敗時返回-1.  
    49.         die(‘could not fork‘);  
    50.     } else if ($pid) {  
    51.         //父進程會得到子進程號,所以這裡是父進程執行的邏輯  
    52.         //如果不需要阻塞進程,而又想得到子進程的退出狀態,則可以注釋掉pcntl_wait($status)語句,或寫成:  
    53.         pcntl_wait($status,WNOHANG); //等待子進程中斷,防止子進程成為殭屍進程。  
    54.     } else {  
    55.         //子進程得到的$pid為0, 所以這裡是子進程執行的邏輯。  
    56.         $path   = $cmd["path"];  
    57.         $pid = $cmd[‘pid‘] ;  
    58.         $total = $cmd[‘total‘] ;  
    59.         echo exec("/usr/bin/php {$path} {$pid} {$total}")."\n";  
    60.         exit(0) ;  
    61.     }  
    62. }  
    63. ?> 

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.