php的多线程使用

PHP 5.3 以上版本,使用pthreads PHP扩展,可以使PHP真正地支持多线程。多线程在处理重复性的循环任务,能够大大缩短程序执行时间。

在liunx下的安装

准备工作:

1.下载Threading for PHP安装包https://github.com/krakjoe/pthreads

2.php安装包

php安装时一定要加上--enable-maintainer-zts参数 这个是安全线程

yum install php-devel php-pear httpd-devel 
wget http://www.php.net/distributions/php-5.5.8.tar.gz
tar zxvf php-5.5.8.tar.gz
cd php-5.5.8.tar.gz
./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-maintainer-zts
make && make install
( make -j3 && make -j3 install) -> Faster building
cp php.ini-development /etc/php.ini
pecl install pthreads
提示错误
error: pthreads requires ZTS, please re-compile PHP with ZTS enabled
不对啊 我加了安全线程参数了 怎么还报错。。。
好像刚才用yum安装php-devel的时候安装了php
于是
yum remove php
重新编译在安装
pecl install pthreads
提示错误 ERROR: `phpize' failed
cd /usr/local/php/bin/
查看是否有phpize
发现有phpize,说明不是php-devel的问题
是安装扩展的方式出了问题,所以php安装是没有问题的
下载pthreads扩展包
unzip pthreads-master.zip
cd pthreads
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
提示Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20121212/
说明安装成功
修改php.ini文件,添加extension=pthreads.so
echo "extension=pthreads.so" >> /etc/php.ini
php -v
提示Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pthreads.so' - /usr/lib64/php/modules/pthreads.so: cannot open shared object file: No such file or directory in Unknown on line 0
尼玛都没一件顺心的安装
错误 我擦
查看一下 /usr/local/php/include/php/main/php_config.php 里面是否有#define ZTS 1
里面有这个啊 我擦 这是什么问题呢 继续百度
重新安装一次试试看
cd php-5.5.8.tar.gz
./configure --prefix=/usr/local/php --with-config-file-path=/etc --enable-maintainer-zts
make -j3 && make -j3 install
cp php.ini-development /etc/php.ini
进入pthreads解压目录
cd ~/soft/pthreads-master
运行phpize脚本
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config

make && make install
提示安装成功
echo "extension=pthreads.so" >> /etc/php.ini
php -v

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib64/php/modules/pthreads.so' - /usr/lib64/php/modules/pthreads.so: undefined symbol: core_globals_id in Unknown on line 0

尼玛还是同样的错误 不知道了

windows下多线程的使用

windows版本的下载地址

http://windows.php.net/downloads/pecl/releases/pthreads/0.1.0/

本人php版本是5.4.17的所以下载php_pthreads-0.1.0-5.4-ts-vc9-x86.zip文件包,其中0.1.0表示为当前pthreads版本号,5.4为php版本号,ts就是之前判断php对应的ts、nts版,vs9代表是Visual Studio 2008 compiler编译器编译的,最后的x86代表的是32位的版本。

将下载好的php_pthreads-0.1.0-5.4-ts-vc9-x86.zip文件包解压找到

pthreadVC2.dll和php_pthreads.dll文件.

1、修改php.ini文件 添加extension=php_pthreads.dll

2、将php_pthreads.dll放到C:\wamp\bin\php\php5.5.12\ext下面

3、将pthreadVC2.dll放到C:\wamp\bin\php\php5.5.12下面

4、将pthreadVC2.dll放到C:\Windows\System32下面

5、将pthreadVC2.dll的绝对路径放到path环境变量里面

3、重启Apache服务器

<?php  
  class test_thread_run extends Thread   
  {  
      public $url;  
      public $data;  
  
      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 : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)';   
      $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);  
      curl_close($curl);  
      return $result;  
  }  
  
  for ($i=0; $i < 10; $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";  
  
  $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";  
?>  

多线程:5.1022920608521 For循环:20.272159099579

多线程花的时间比单线程花的时间少的多。

windows下多线程php测试成功!!!