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

通过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. }
  28. function addthread($func)
  29. {
  30. $args = array_slice(func_get_args(), 1);
  31. $this->hooks[] = $func;
  32. $this->args[] = $args;
  33. return true;
  34. }
  35. function runthread()
  36. {
  37. if(isset($_GET['flag']))
  38. {
  39. $flag = intval($_GET['flag']);
  40. }
  41. if($flag || $flag === 0)
  42. {
  43. call_user_func_array($this->hooks[$flag], $this->args[$flag]);
  44. }
  45. else
  46. {
  47. for($i = 0, $size = count($this->hooks); $i < $size; $i++)
  48. {
  49. $fp=fsockopen($_SERVER['HTTP_HOST'],$_SERVER['SERVER_PORT']);
  50. if($fp)
  51. {
  52. $out = "GET {$_SERVER['PHP_SELF']}?flag=$i HTTP/1.1rn";
  53. $out .= "Host: {$_SERVER['HTTP_HOST']}rn";
  54. $out .= "Connection: Closernrn";
  55. fputs($fp,$out);
  56. fclose($fp);
  57. }
  58. }
  59. }
  60. }
  61. }

使用方法,代码如下:

  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. while($active and $mrc == CURLM_OK){
  45. if(curl_multi_select($mh) != -1){
  46. do{
  47. $mrc=curl_multi_exec($mh,$active);
  48. }while($mrc == CURLM_CALL_MULTI_PERFORM);
  49. }
  50. }
  51. $res=array();
  52. foreach($this->url_list as $i => $url){
  53. $res[$i]=curl_multi_getcontent($conn[$i]);
  54. curl_close($conn[$i]);
  55. curl_multi_remove_handle($mh,$conn[$i]);//释放资源
  56. }
  57. curl_multi_close($mh);
  58. return $res;
  59. }
  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. }