关于php多线程的记录

  最近需要对3W台服务器进行下发脚本,如果一个一个执行,时间大约在2个小时,特别的慢,于是修改程序,采用php的多线程去分发,大概在10分钟左右完成,下面记录下这次的经验和理解:

  我所理解的php的多线程实现的方式有两种,下面是官方的介绍:

  1、官方的介绍:(转载自张宴的博客)

  到php5.3以上的版本,php才算是真正的支持多线程,使用的是pthreads的扩展,php在处理多个循环的任务的时候,能够大大的缩短程序的执行时间。

  大多数网站的性能瓶颈,不在php的服务器上,而是在mysql服务器上。因为可以通过横向的增加服务器或者是cpu的核数来应对。如果用Mysql数据库,一条联合查询可能处理玩很复杂的业务逻辑,但是遇到大并发量的查询,服务器可能就会瘫痪。因此我们可以使用NoSQL数据库代替mysql服务器,一条很复杂的sql语句,可能需要好几条NOSQL语句来完成,但是遇到大量的并发的情况下,速度确实非常明显的。然后再加上php的多线程的使用,通过十个php的线程查询NOSQL,然后汇总返回结果输出,速度是非常之快的。

1   PHP扩展下载:https://github.com/krakjoe/pthreads
2   PHP手册文档:http://php.net/manual/zh/book.pthreads.php

  1、扩展的编译安装(Linux),编辑参数 --enable-maintainer-zts 是必选项:

 1 cd /Data/tgz/php-5.5.1
 2 ./configure --prefix=/Data/apps/php --with-config-file-path=/Data/apps/php/etc --with-mysql=/Data/apps/mysql --with-mysqli=/Data/apps/mysql/bin/mysql_config --with-iconv-dir --with-freetype-dir=/Data/apps/libs --with-jpeg-dir=/Data/apps/libs --with-png-dir=/Data/apps/libs --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt=/Data/apps/libs --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-opcache --with-pdo-mysql --enable-maintainer-zts
 3 make clean
 4 make
 5 make install        
 6 
 7 unzip pthreads-master.zip
 8 cd pthreads-master
 9 /Data/apps/php/bin/phpize
10 ./configure --with-php-config=/Data/apps/php/bin/php-config
11 make
12 make install
vi /Data/apps/php/etc/php.ini
extension = "pthreads.so"

  2、给出一段PHP多线程、与For循环,抓取百度搜索页面的PHP代码示例:

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

  上述是官方给的一个php多线程的一个扩展,由于需要重新编译,因此自己没有进行试验,但其实本质都是在linux中开启多个线程来进行处理。

2、下面说下我自己搞得伪造的php的多线程的实例:

下面是一段测试的代码:

for.php

 1 <?php
 2 $start = microtime(true);
 3 for($i=0;$i<5000;$i++){                                                                                                  
 4     $cmd = "php ./test.php &"; -----// 主要是这个,使用php执行,将程序放到后台执行,不等待返回结果,再次继续循环,类似于异步的方式,生成多个php的线程。
 5     $fp = popen($cmd,"r");
 6 }
 7 fclose($fp);
 8 $end = microtime(true);
 9  
10 echo $end-$start;
11  
12  
13 ?>

test.php(简单的显示phpinfo的信息):

1 <?php
2     phpinfo();
3 ?>
采用多线程的执行的时间:32.89448595047
采用普通的执行的时间:85.191102027893

执行循环的次数越大,这种时间差就很明显。

ps:我们可以写一个python或者shell的脚本,来实时的检测php的进程数的变化:

1 #!/usr/bin/env/ python
2 import os
3 from time import sleep
4 
5 while 1:
6     os.system("ps -aux|grep php|wc -l");
7     sleep(1);
8 
9 上面是一个简单的统计php进程数的一个脚本,在shell界面执行的时候,可以将输出定位到一个文件中。

记录:

并发的最大记录数:

  之前在公司的机器上跑了一个多线程的程序,

  机器的配置是48G内存   6个CPU,

  一次并发的线程数大概在3000左右,超过4000,会造成内存溢出,机器挂死。这是最大的并发线程数。

但是我们在线上跑的时候,为了安全,一定要记住最多我们也就并发几十个到100个进程,如果超出了范围,会发生数据的丢失和一些诡异的现象发生。

原文地址:https://www.cnblogs.com/shangzekai/p/4449765.html