首頁  >  文章  >  後端開發  >  php如何多線程

php如何多線程

王林
王林原創
2019-09-30 17:44:143469瀏覽

php如何多線程

PHP开启多线程的方法

php如何安装pthreads的拓展的,我采用的是windows安装,我本机的开发环境是phpstudy。

有几点特别需要注意,在window中此类拓展一定是要在线程安全(ts)的php版本中运行。

安装

php如何多線程

1、复制php_pthreads.dll 到目录 bin\php\ext\ 

2、复制pthreadVC2.dll 到目录 C:\windows\system32 下面。

3、打开php配置文件php.ini,在后面加上extension=php_pthreads.dll。

提示!

Windows系统需要将 pthreadVC2.dll 所在路径加入到 PATH 环境变量中。我的电脑--->鼠标右键--->属性--->高级--->环境变量--->系统变量--->找到名称为Path的--->编辑--->在变量值最后面加上pthreadVC2.dll的完整路径。

测试

测试脚本我复制的是http://zyan.cc/pthreads/这里的实例代码。

php如何多線程

这里我贴上我的代码,就是在子线程中我添加了日志记录,判断下他的返回值如何

<?php  
set_time_limit(0);
  class test_thread_run extends Thread   
  {  
      public $url;  
      public $data=&#39;init&#39;;  
  
      public function __construct($url)  
      {  
          $this->url = $url;  
      }  
  
      public function run()  
      {  
          if(($url = $this->url))  
          {  
              $this->data = model_http_curl_get($url);  
          }  
      }  
  }  
  
  function model_thread_result_get($urls_array)   
  {  
      foreach ($urls_array as $key => $value)   
      {  
          $thread_array[$key] = new test_thread_run($value["url"]);  
          $thread_array[$key]->start();  
      }  
  
      foreach ($thread_array as $thread_array_key => $thread_array_value)   
      {  
          while($thread_array[$thread_array_key]->isRunning())  
          {  
              usleep(10);  
          }  
          if($thread_array[$thread_array_key]->join())  
          {  
              $variable_data[$thread_array_key] = $thread_array[$thread_array_key]->data;  
          }  
      }  
      return $variable_data;  
  }  
  
  function model_http_curl_get($url,$userAgent="")   
  {  
      $userAgent = $userAgent ? $userAgent : &#39;Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)&#39;;   
      $curl = curl_init();  
      curl_setopt($curl, CURLOPT_URL, $url);  
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
      curl_setopt($curl, CURLOPT_TIMEOUT, 5);  
      curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);  
      $result = curl_exec($curl);  
      if($result=== false){
          $result = curl_error($curl);
      }
      curl_close($curl);
      $pid = Thread::getCurrentThreadId();
      $file=&#39;D:\\pid\\&#39;.$pid.&#39;p.txt&#39;;
      
      file_put_contents($file,$result);
      return $result;  
  }  
  
  for ($i=0; $i < 100; $i++)   
  {   
      $urls_array[] = array("name" => "baidu", "url" => "http://www.baidu.com/s?wd=".mt_rand(10000,20000));  
  }  
  
  $t = microtime(true);  
  $result = model_thread_result_get($urls_array);
  
  $e = microtime(true);  
  echo "多线程:".($e-$t)."\n";  
  //print_r($result);
  $t = microtime(true);  
  foreach ($urls_array as $key => $value)   
  {  
      $result_new[$key] = model_http_curl_get($value["url"]);  
  }  
  $e = microtime(true);  
  echo "For循环:".($e-$t)."\n";  
 //print_r($result_new);
?>

结果在我的目录下:

php如何多線程

里面的文本内容是:

php如何多線程

也就是说子线程是正常调度了,但是没有拿到数据。此类情况可能是我的网速的原因,垃圾8M电信。

推荐教程:PHP视频教程

以上是php如何多線程的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn