当前位置:首页 > PHP教程 > php环境安装 > 列表

php多线程安装pthreads步骤详解

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-25 15:16:35 浏览: 评论:0 

php多线程安装pthreads方法有一点复杂了,下面的安装步骤有一些多,希望能帮助到各位.

PHP扩展下载:https://github.com/krakjoe/pthreads

PHP手册文档:http://php.net/manual/zh/book.pthreads.php

安装脚本,代码如下:

  1. #!/bin/sh 
  2. cd /web/soft/php 
  3. if [ -d "pthreads-master" ];then 
  4. rm -rf pthreads-master 
  5. fi 
  6. unzip pthreads-master.zip 
  7. cd pthreads-master 
  8. /web/server/php/bin/phpize 
  9. ./configure --with-php-config=/web/server/php/bin/php-config 
  10. make 
  11. make install 
  12. rm -rf pthreads-master 
  13. PHPINI="/web/server/php/etc/php.ini" 
  14. sed -i '907a extension = "pthreads.so"' $PHPINI 
  15. #更新php-fpm配置 
  16. sed -i 's%;pid = run/php-fpm.pid%pid = run/php-fpm.pid%' /web/server/php/etc/php-fpm.conf 
  17. sed -i 's%;error_log = log/php-fpm.log%error_log = log/php-fpm.log%' /web/server/php/etc/php-fpm.conf 
  18. #杀死php-fpm进程 
  19. ps aux | grep "php" | grep -v "grep" | awk '{print $2}' | xargs -i kill -9 {} 
  20. #启动php-fpm 
  21. /web/server/php/sbin/php-fpm 

在安装过程中出现错误:

configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers

解决方法是安装或升级re2c 0.13.4以上版本。

下面我们用rpm包安装此库:

  1. centos-5 32位:http://pkgs.repoforge.org/re2c/re2c-0.13.5-1.el5.rf.i386.rpm 
  2. centos-5 64位:http://pkgs.repoforge.org/re2c/re2c-0.13.5-1.el5.rf.x86_64.rpm 
  3. centos-6 32位:http://pkgs.repoforge.org/re2c/re2c-0.13.5-1.el6.rf.i686.rpm 
  4. centos-6 64位:http://pkgs.repoforge.org/re2c/re2c-0.13.5-1.el6.rf.x86_64.rpm 
  5. configure: error: pthreads requires ZTS, please re-compile PHP with ZTS enabled 

原因:我在编译php的时候没有加入 --enable-maintainer-zts,这个必须要重新编译php,不能动态加载的.

于是我重新编译了php,在原来的编译参数基础上那个加入了 --enable-maintainer-zts ,重新编译安装php即可.

以下为一个示例,代码如下:

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

Tags: php多线程安装 pthreads安装

分享到: