国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > PHP > 正文

php多進(jìn)程幾個(gè)例子

2024-05-04 21:47:47
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

php多進(jìn)程這個(gè)東西先是在java中有不過(guò)現(xiàn)在高版本的php也支持多進(jìn)程這個(gè)功能,但經(jīng)過(guò)測(cè)試性能不如j(m.survivalescaperooms.com)ava了希望后期有所提高了,下面我們一起來(lái)看看我整理了幾個(gè)關(guān)于php多進(jìn)程例子,希望能幫助你理解多線程.

php多進(jìn)程的實(shí)現(xiàn)依賴于pcntl擴(kuò)展,編譯PHP的時(shí)候,可以加上’–enable-pcntl’或者也可以單獨(dú)編譯.

有三點(diǎn)需要注意:

1.子進(jìn)程不在執(zhí)行fork之前的代碼,只是把父進(jìn)程的內(nèi)存狀況復(fù)制一份新的,所以,關(guān)于子進(jìn)程的個(gè)性化設(shè)置需要單獨(dú)設(shè)置.

2.輸出重定向,程序中使用echo,或造成命令行的混亂,影響分辨,可以用ob_start重定向到log文件,當(dāng)然,你直接使用log是更好的辦法,此實(shí)例中l(wèi)og文件,按照進(jìn)程pid分組.

3.父進(jìn)程沒有代碼執(zhí)行,將可能提前退出,子進(jìn)程可能成為孤兒進(jìn)程.

demo接收:

用10個(gè)子進(jìn)程來(lái)處理輸出任務(wù),任務(wù)總量是1000,然后,按照任務(wù)數(shù)平均分到十個(gè)子進(jìn)程當(dāng)中去,代碼如下:

  1.  //輸出重定向到log文件                    
  2. function echo_to_log($content){ 
  3.     global $current_pid
  4.     $logfile =  __FILE__ . $current_pid .  '.log'
  5.     $fp = fopen($logfile'a+'); 
  6.     fwrite($fp$content); 
  7.     fclose($fp); 
  8.                                                                                   
  9. ob_start('echo_to_log'); 
  10. //獲取當(dāng)前進(jìn)程pid 
  11. $current_pid = getmypid(); 
  12. $fork_nums = 10; 
  13. $total = 1000; 
  14.                                                                                   
  15. for($i = 0; $i < $fork_nums$i++){ 
  16.     $pid = pcntl_fork(); 
  17.     //等于0時(shí),是子進(jìn)程 
  18.     if($pid == 0){ 
  19.         $current_pid = $pid
  20.         do_task($i); 
  21.      //大于0時(shí),是父進(jìn)程,并且pid是產(chǎn)生的子進(jìn)程的PID 
  22.     } else if($pid > 0) { 
  23.     } 
  24.                                                                                   
  25. //任務(wù)函數(shù) 
  26. function do_task($task_num){ 
  27.     global $total
  28.     $start = $total / 10 * $task_num
  29.     $end = $total / 10 * ($task_num + 1); 
  30.     for(;$start<$end;$start++){ 
  31.         echo $task_num . " " . $start . "\n"
  32.     } 
  33.     //子進(jìn)程執(zhí)行完任務(wù)以后終止,當(dāng)然你可以返回主進(jìn)程的代碼部分做相關(guān)操作。 
  34.     exit(); 

多進(jìn)程控制的框架代碼,留著備查,代碼如下:

  1. declare(ticks=1); 
  2. function sigHandler($signal
  3.     echo "a child exited\n"
  4. pcntl_signal(SIGCHLD, sigHandler, false); 
  5. echo "this is " . posix_getpid() . PHP_EOL; 
  6. for($i=0; $i<3; $i++) 
  7.     $pid = pcntl_fork(); 
  8.     if($pid == -1)  
  9.     {    
  10.         echo 'fork failed ' . PHP_EOL; 
  11.     }    
  12.     else if($pid
  13.     {    
  14.     }    
  15.     else 
  16.     {    
  17.         $pid = posix_getpid(); 
  18.         echo 'child ' . $pid . ' ' . time() . PHP_EOL; 
  19.         sleep(rand(2,5)); 
  20.         echo 'child ' . $pid . ' done ' . time() . PHP_EOL; 
  21.         exit(0); 
  22.     }    
  23. do 
  24.     $pid = pcntl_wait($status);  
  25.     echo 'child quit ' . $pid . PHP_EOL; 
  26. }while($pid > 0);  
  27. echo 'parent done' . PHP_EOL; 

例子,給出一段PHP多線程、與For循環(huán),抓取百度搜索頁(yè)面的PHP代碼示例,代碼如下:

  1. <?php 
  2.   class test_thread_run extends Thread  
  3.   { 
  4.       public $url
  5.       public $data
  6.  
  7.       public function __construct($url
  8.       { 
  9.           $this->url = $url
  10.       } 
  11.  
  12.       public function run() 
  13.       { 
  14.           if(($url = $this->url)) 
  15.           { 
  16.               $this->data = model_http_curl_get($url); 
  17.           } 
  18.       } 
  19.   } 
  20.  
  21.   function model_thread_result_get($urls_array)  
  22.   { 
  23.       foreach ($urls_array as $key => $value)  
  24.       { 
  25.           $thread_array[$key] = new test_thread_run($value["url"]); 
  26.           $thread_array[$key]->start(); 
  27.       } 
  28.  
  29.       foreach ($thread_array as $thread_array_key => $thread_array_value)  
  30.       { 
  31.           while($thread_array[$thread_array_key]->isRunning()) 
  32.           { 
  33.               usleep(10); 
  34.           } 
  35.           if($thread_array[$thread_array_key]->join()) 
  36.           { 
  37.               $variable_data[$thread_array_key] = $thread_array[$thread_array_key]->data; 
  38.           } 
  39.       } 
  40.       return $variable_data
  41.   } 
  42.  
  43.   function model_http_curl_get($url,$userAgent="")  
  44.   { 
  45.       $userAgent = $userAgent ? $userAgent : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)';  
  46.       $curl = curl_init(); 
  47.       curl_setopt($curl, CURLOPT_URL, $url); 
  48.       curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  49.       curl_setopt($curl, CURLOPT_TIMEOUT, 5); 
  50.       curl_setopt($curl, CURLOPT_USERAGENT, (m.survivalescaperooms.com)$userAgent); 
  51.       $result = curl_exec($curl); 
  52.       curl_close($curl); 
  53.       return $result
  54.   } 
  55.  
  56.   for ($i=0; $i < 100; $i++)  
  57.   {  
  58.       $urls_array[] = array("name" => "baidu""url" => "http://m.survivalescaperooms.com/s?wd=".mt_rand(10000,20000)); 
  59.   } 
  60.  
  61.   $t = microtime(true); 
  62.   $result = model_thread_result_get($urls_array); 
  63.   $e = microtime(true); 
  64.   echo "多線程:".($e-$t)."\n"
  65.  
  66.   $t = microtime(true); 
  67.   foreach ($urls_array as $key => $value)  
  68.   { 
  69.       $result_new[$key] = model_http_curl_get($value["url"]); 
  70.   } 
  71.   $e = microtime(true); 
  72.   echo "For循環(huán):".($e-$t)."\n"
  73. ?> 

PHP多線程類,代碼如下:

  1. /** 
  2.  * @title:  PHP多線程類(Thread) 
  3.  * @version: 1.0 
  4.  * @author:   < web@ > 
  5.  * @published: 2010-11-2 
  6.  *  
  7.  * PHP多線程應(yīng)用示例: 
  8.  *  require_once 'thread.class.php'; 
  9.  *  $thread = new thread(); 
  10.  *  $thread->addthread('action_log','a'); 
  11.  *  $thread->addthread('action_log','b'); 
  12.  *  $thread->addthread('action_log','c'); 
  13.  *  $thread->runthread(); 
  14.  *   
  15.  *  function action_log($info) { 
  16.  *   $log = 'log/' . microtime() . '.log'; 
  17.  *   $txt = $info . "rnrn" . 'Set in ' . Date('h:i:s', time()) . (double)microtime() . "rn"; 
  18.  *   $fp = fopen($log, 'w'); 
  19.  *   fwrite($fp, $txt); 
  20.  *   fclose($fp); 
  21.  *  } 
  22.  */ 
  23. class thread { 
  24.  
  25.     var $hooks = array(); 
  26.     var $args = array(); 
  27.      
  28.     function thread() { 
  29.     } 
  30.      
  31.     function addthread($func
  32.     { 
  33.      $args = array_slice(func_get_args(), 1); 
  34.      $this->hooks[] = $func
  35.   $this->args[] = $args
  36.   return true; 
  37.     } 
  38.      
  39.     function runthread() 
  40.     { 
  41.      if(isset($_GET['flag'])) 
  42.      { 
  43.       $flag = intval($_GET['flag']); 
  44.      } 
  45.      if($flag || $flag === 0) 
  46.   { 
  47.    call_user_func_array($this->hooks[$flag], $this->args[$flag]); 
  48.   } 
  49.      else  
  50.      { 
  51.          for($i = 0, $size = count($this->hooks); $i < $size$i++) 
  52.          { 
  53.           $fp=fsockopen($_SERVER['HTTP_HOST'],$_SERVER['SERVER_PORT']); 
  54.           if($fp
  55.           { 
  56.            $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1rn"
  57.            $out .= "Host: {$_SERVER['HTTP_HOST']}rn"
  58.            $out .= "Connection: Closernrn"
  59.                  fputs($fp,$out); 
  60.                  fclose($fp); 
  61.           } 
  62.          } 
  63.      } 
  64.     } 

使用方法,代碼如下:

  1. $thread = new thread(); 
  2. $thread->addthread('func1','info1'); 
  3. $thread->addthread('func2','info2'); 
  4. $thread->addthread('func3','info3'); 
  5. $thread->runthread(); 

說(shuō)明:addthread是添加線程函數(shù),第一個(gè)參數(shù)是函數(shù)名,之后的參數(shù)(可選)為傳遞給指定函數(shù)的參數(shù).

runthread是執(zhí)行線程的函數(shù).

在linux系統(tǒng)中需要配置安裝一下pthreads

1、擴(kuò)展的編譯安裝(Linux),m.survivalescaperooms.com 編輯參數(shù) --enable-maintainer-zts 是必選項(xiàng),代碼如下:

  1. cd /Data/tgz/php-5.5.1 
  2. ./configure --prefix=/Data/apps/php --with-config-file-path=/Data/apps/php/etc --with-mysql=/Data/apps/mysql --with-mysqli=/Data/apps/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir=/Data/apps/libs --with-jpeg-dir=/Data/apps/libs --with-png-dir=/Data/apps/libs --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/Data/apps/libs --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-opcache --with-pdo-mysql --enable-maintainer-zts 
  3. make clean 
  4. make 
  5. make install        
  6.  
  7. unzip pthreads-master.zip 
  8. cd pthreads-master 
  9. /Data/apps/php/bin/phpize 
  10. ./configure --with-php-config=/Data/apps/php/bin/php-config 
  11. make 
  12. make install 
  13. vi /Data/apps/php/etc/php.ini 

添加如下代碼:

extension = "pthreads.so" 

PHP擴(kuò)展下載:https://github.com/krakjoe/pthreads

PHP手冊(cè)文檔:http://php.net/manual/zh/book.pthreads.php

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 苏州市| 天峨县| 游戏| 阿拉善右旗| 德保县| 新安县| 昌黎县| 宁河县| 游戏| 西华县| 汤原县| 盐津县| 沙洋县| 屯门区| 绥滨县| 丹棱县| 西乌珠穆沁旗| 汉阴县| 科尔| 新营市| 高陵县| 乐亭县| 远安县| 漯河市| 凉城县| 富源县| 肇州县| 晋宁县| 化州市| 阿拉善右旗| 页游| 南阳市| 麻城市| 林州市| 临颍县| 嘉定区| 南雄市| 滦南县| 郑州市| 德昌县| 海城市|