当前位置:首页 > PHP教程 > php类库 > 列表

PHP多线程的实例,PHP多线程类

发布:smiling 来源: PHP粉丝网  添加日期:2014-07-01 14:17:21 浏览: 评论:0 

通过WEB服务器来实现PHP多线程功能,当然,对多线程有深入理解的人都知道通过WEB服务器实现的多线程只能模仿多线程的一些效果,并不是真正意义上的多线程.

但不管怎么样,它还是能满足我们的一些需要的,在需要类似多线程的功能方面还是可以采用这个类,代码如下:

  1. /** 
  2. * @title: PHP多线程类(Thread) 
  3. * @version: 1.0 
  4. * @author: phper.org.cn < web@phper.org.cn > 
  5. * @published: 2010-11-2 
  6. * 
  7. * PHP多线程应用示例: 
  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. var $hooks = array(); 
  25. var $args = array(); 
  26. function thread() { 
  27. function addthread($func
  28. $args = array_slice(func_get_args(), 1); 
  29. $this->hooks[] = $func
  30. $this->args[] = $args
  31. return true; 
  32. function runthread() 
  33. if(isset($_GET['flag'])) 
  34. $flag = intval($_GET['flag']); 
  35. if($flag || $flag === 0) 
  36. call_user_func_array($this->hooks[$flag], $this->args[$flag]); 
  37. else 
  38. for($i = 0, $size = count($this->hooks); $i < $size$i++) 
  39. $fp=fsockopen($_SERVER['HTTP_HOST'],$_SERVER['SERVER_PORT']); 
  40. if($fp
  41. $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1rn"
  42. $out .= "Host: {$_SERVER['HTTP_HOST']}rn"
  43. $out .= "Connection: Closernrn"
  44. fputs($fp,$out); 
  45. fclose($fp); 

使用方法,代码如下:

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

说明:

addthread() 是添加线程函数,第一个参数是函数名,之后的参数(可选)为传递给指定函数的参数.

runthread() 是执行线程的函数.

PHP实例:利用curl实现多线程下载图片

其实是php利用curl实现的一个多线程类,有了这个类,我们也可利用该类执行多线程任务了,代码如下:

  1. class curl_multi{  
  2.     private $url_list=array();  
  3.     private $curl_setopt=array(  
  4.         'CURLOPT_RETURNTRANSFER' => 1,//结果返回给变量  
  5.         'CURLOPT_HEADER' => 0,//是否需要返回HTTP头  
  6.         'CURLOPT_NOBODY' => 0,//是否需要返回的内容  
  7.         'CURLOPT_FOLLOWLOCATION' => 0,//自动跟踪  
  8.         'CURLOPT_TIMEOUT' => 6//超时时间(s)  
  9.     );  
  10.     function __construct($seconds=30){  
  11.         set_time_limit($seconds);  
  12.     }  
  13.     /*  
  14.      * 设置网址  
  15.      * @list 数组  
  16.      */  
  17.     public function setUrlList($list=array()){  
  18.         $this->url_list=$list;  
  19.     }  
  20.     /*  
  21.      * 设置参数  
  22.      * @cutPot array  
  23.      */  
  24.     public function setOpt($cutPot){  
  25.         $this->curl_setopt=$cutPot+$this->curl_setopt;  
  26.     }  
  27.     /*  
  28.      * 执行  
  29.      * @return array  
  30.      */  
  31.     public function execute(){  
  32.         $mh=curl_multi_init();  
  33.         foreach($this->url_list as $i=>$url){  
  34.             $conn[$i]=curl_init($url);  
  35.             foreach($this->curl_setopt as $key => $val){  
  36.                 curl_setopt($conn[$i],preg_replace('/(CURLOPT_w{1,})/ie','$0',$key),$val);  
  37.             }  
  38.             curl_multi_add_handle($mh,$conn[$i]);  
  39.         }  
  40.         $active=false;  
  41.         do{  
  42.             $mrc=curl_multi_exec($mh,$active);  
  43.         }while($mrc == CURLM_CALL_MULTI_PERFORM);  
  44.   
  45.         while($active and $mrc == CURLM_OK){  
  46.             if(curl_multi_select($mh) != -1){  
  47.                 do{  
  48.                     $mrc=curl_multi_exec($mh,$active);  
  49.                 }while($mrc == CURLM_CALL_MULTI_PERFORM);  
  50.             }  
  51.         }  
  52.         $res=array();  
  53.         foreach($this->url_list as $i => $url){  
  54.             $res[$i]=curl_multi_getcontent($conn[$i]);  
  55.             curl_close($conn[$i]);  
  56.             curl_multi_remove_handle($mh,$conn[$i]);//释放资源    
  57.         }  
  58.         curl_multi_close($mh);  
  59.         return $res;  
  60.     }  

php使用多线程下载类示例,下载远程图片,代码如下:

  1. $curl_mul=new curl_multi();  
  2.     $curl_mul->setUrlList(array('http://www.phpfensi.com/img/logo.jpg','http://www.phpfensi.com/img/logo.jpg','http://www.phpfensi.com/img/logo.jpg'));  
  3.     $a=$curl_mul->execute();  
  4.     $i=1;  
  5.     foreach($a as $v){  
  6.         $filename=$i.'.gif';  
  7.         $fp2=@fopen($filename,'a');  
  8.         fwrite($fp2,$v);  
  9.         fclose($fp2);  
  10.         $i++;  
  11.     } 

Tags: PHP多线程 PHP多线程类

分享到:

相关文章