您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP多线程的使用_PHP教程

2024/3/14 12:48:32发布20次查看
大多数网站的性能瓶颈不在php服务器上,因为它可以简单地通过横向增加服务器或cpu核数来轻松应对(对于各种云主机,增加vps或cpu核数就更方便了,直接以备份镜像增加vps,连操作系统、环境都不用安装配置),而是在于mysql数据库。如果用 mysql 数据库,一条联合查询的sql,也许就可以处理完业务逻辑,但是,遇到大量并发请求,就歇菜了。如果用 nosql 数据库,也许需要十次查询,才能处理完同样地业务逻辑,但每次查询都比 mysql 要快,十次循环nosql查询也许比一次mysql联合查询更快,应对几万次/秒的查询完全没问题。如果加上php多线程,通过十个线程同时查询nosql,返回结果汇总输出,速度就要更快了。我们实际的app产品中,调用一个通过用户喜好实时推荐商品的php接口,php需要对bigsea nosql数据库发起500~1000次查询,来实时算出用户的个性喜好商品数据,php多线程的作用非常明显。
php扩展下载: https://github.com/krakjoe/pthreads 
php手册文档: http://php.net/manual/zh/book.pthreads.php
php多线程的实现
扩展的编译安装(linux),编辑参数 --enable-maintainer-zts 是必选项: cd /data/tgz/php-5.5.1 
./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 
make clean 
make 
make install
unzip pthreads-master.zip 
cd pthreads-master 
/data/apps/php/bin/phpize 
./configure --with-php-config=/data/apps/php/bin/php-config 
make 
make install
vi /data/apps/php/etc/php.ini
添加: 
extension = pthreads.so
给出一段php多线程、与for循环,抓取百度搜索页面的php代码示例   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    {          $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;   ?>  
原文:http://blog.s135.com/pthreads/  张宴
http://www.bkjia.com/phpjc/739145.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/739145.htmltecharticle大多数网站的性能瓶颈不在php服务器上,因为它可以简单地通过横向增加服务器或cpu核数来轻松应对(对于各种云主机,增加vps或cpu核数就...
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product