嘗試php命令列指令碼多進程並發執行

來源:互聯網
上載者:User

標籤:style   color   os   使用   io   ar   for   檔案   div   

php不支援多線程,但是我們可以把問題轉換成“多進程”來解決。由於php中的pcntl_fork只有unix平台才可以使用,所以本文嘗試使用popen來替代。 
 
下面是一個例子:
  
被並行調用的子程式:

  1. <?php 
  2. if($argc==1){ 
  3.          echo("argv\n"); 
  4. } 
  5. $arg = $argv[1];     
  6. for($i=0; $i<10; $i++) 
  7. { 
  8. echo($i.".1.".time()." exec $arg \n"); 
  9. if($arg==‘php2‘) 
  10. { 
  11. sleep(1); 
  12. echo($i.".2.".time()." exec $arg \n"); 
  13. sleep(1); 
  14. }else{ 
  15. sleep(1); 
  16. } 
  17. ?> 

----------------------------
主調用者程式,由他調用子進程,同時並發的收集子程式的輸出 

 

  1. <?php 
  2. error_reporting(E_ALL); 
  3.      
  4. $handle1 = popen(‘php sub.php php1‘, ‘r‘); 
  5. $handle2 = popen(‘php sub.php php2‘, ‘r‘); 
  6. $handle3 = popen(‘php sub.php php3‘, ‘r‘); 
  7.      
  8. echo "‘$handle1‘; " . gettype($handle1) . "\n"; 
  9. echo "‘$handle2‘; " . gettype($handle2) . "\n"; 
  10. echo "‘$handle3‘; " . gettype($handle3) . "\n"; 
  11.       
  12. //sleep(20); 
  13. while(!feof($handle1) || !feof($handle2) || !feof($handle3) ){ 
  14. $read = fgets($handle1); 
  15. echo $read; 
  16.       
  17. $read = fgets($handle2); 
  18. echo $read; 
  19.       
  20. $read = fgets($handle3); 
  21. echo $read; 
  22. } 
  23.  
  24.  
  25. pclose($handle1); 
  26. pclose($handle2); 
  27. pclose($handle3); 
  28. ?> 

-------------------

下面是我機器上的輸出:


> php exec.php
‘Resource id #4‘; resource
‘Resource id #5‘; resource
‘Resource id #6‘; resource
0.1.1147935331 exec php1
0.1.1147935331 exec php2
0.1.1147935331 exec php3
1.1.1147935332 exec php1
0.2.1147935332 exec php2
1.1.1147935332 exec php3
2.1.1147935333 exec php1
1.1.1147935333 exec php2
2.1.1147935333 exec php3
3.1.1147935334 exec php1
1.2.1147935334 exec php2
3.1.1147935334 exec php3
4.1.1147935335 exec php1
2.1.1147935335 exec php2
4.1.1147935335 exec php3
5.1.1147935336 exec php1
2.2.1147935336 exec php2
5.1.1147935336 exec php3
6.1.1147935337 exec php1
3.1.1147935337 exec php2
6.1.1147935337 exec php3
7.1.1147935338 exec php1
3.2.1147935338 exec php2
7.1.1147935338 exec php3
8.1.1147935339 exec php1
4.1.1147935339 exec php2
8.1.1147935339 exec php3
9.1.1147935340 exec php1
4.2.1147935340 exec php2
9.1.1147935340 exec php3
5.1.1147935341 exec php2
5.2.1147935342 exec php2
6.1.1147935343 exec php2
6.2.1147935344 exec php2
7.1.1147935345 exec php2
7.2.1147935346 exec php2
8.1.1147935347 exec php2
8.2.1147935348 exec php2
9.1.1147935349 exec php2
9.2.1147935350 exec php2

**總結:**

**主程式迴圈等待子進程, 通過fgets或fread 把子進程的輸出擷取出來 , 從時間戳記上看,的確實現了並發執行。**
  
-----------------------------------------------
改進:
  
*  popen開啟的控制代碼是單向的,如果需要向子進程互動,可以使用proc_open
*  使用數組和子函數代替while(!feof($handle1) || !feof($handle2) || !feof($handle3) )這種齷齪的寫法
*  用fread一次把子進程已經產生的輸出取完,而不是每次一行。


這是另一個改進:
一個並發執行shell任務的調度者,本程式讀取一個任務檔案,把裡面的每行命令並發執行, 可以設定同時存在的子進程數目:

  1. <? 
  2. /* 
  3.          主工作管理員 
  4.          並發的執行子任務列表 
  5. */ 
  6.  
  7. include("../common/conf.php"); 
  8. include("../common/function.php"); 
  9.  
  10. //開啟的進程數 
  11. $exec_number = 40 ; 
  12.  
  13. /***** main ********/ 
  14. if($argc==1){ 
  15.          echo("argv\n"); 
  16. } 
  17. $taskfile = $argv[1]; 
  18.  
  19.  
  20. //tasklist 
  21. $tasklist = file($taskfile); 
  22.  
  23.  
  24. $tasklist_len = count($tasklist); 
  25. $tasklist_pos = 0; 
  26.  
  27.  
  28. $handle_list = array(); 
  29.  
  30.  
  31. while(1){ 
  32.  
  33.  
  34.          //子進程列表有空閑,則填充補齊子進程列表 
  35.          if($exec_number > count($handle_list) && 
  36.                              $tasklist_pos < $tasklist_len) 
  37.          { 
  38.                  for($i=$tasklist_pos; $i<$tasklist_len; ) 
  39.                      { 
  40.                              $command = $tasklist[$i] ; 
  41.                              $handle_list[] = popen($command , "r" ); 
  42.                              tolog("begin task \t ".$tasklist[$i]); 
  43.                              $i++; 
  44.                              if($exec_number == count($handle_list)) break; 
  45.                      } 
  46.                      $tasklist_pos = $i; 
  47.          } 
  48.  
  49.  
  50.          //如果子進程列表空,退出 
  51.          if(0 == count($handle_list)) 
  52.          { 
  53.                      break; 
  54.          } 
  55.  
  56.  
  57.          //檢查子進程列表的輸出,把停掉的子進程關閉並記錄下來 
  58.          $end_handle_keys = array(); 
  59.          foreach($handle_list as $key => $handle) 
  60.          { 
  61.                      //$str = fgets($handle, 65536); 
  62.                      $str = fread($handle, 65536); 
  63.                      echo($str); 
  64.  
  65.  
  66.                      if(feof($handle)) 
  67.                      { 
  68.                              $end_handle_keys[] = $key; 
  69.                              pclose($handle); 
  70.                      } 
  71.          } 
  72.  
  73.  
  74.          //踢出停掉的子進程 
  75.          foreach($end_handle_keys as $key) 
  76.          { 
  77.                      unset($handle_list[$key]); 
  78.                      //var_dump($handle_list); 
  79.                      //exit; 
  80.          } 
  81.  
  82.  
  83.  
  84. } 
  85.  
  86. tolog("\n\n*******************end**********************\n\n", "" ,     true); 
  87. ?>

 

嘗試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.