首页 / PHP / PHP多线程的使用_PHP教程
PHP多线程的使用_PHP教程
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP多线程的使用_PHP教程,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3813字,纯文字阅读大概需要6分钟。
内容图文
![PHP多线程的使用_PHP教程](/upload/InfoBanner/zyjiaocheng/182/31128340f0b541688ddf81ce67f5ae80.jpg)
PHP扩展下载: https://github.com/krakjoe/pthreads
PHP手册文档: http://php.com/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代码示例
- 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 < 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";
- $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核数就...
内容总结
以上是互联网集市为您收集整理的PHP多线程的使用_PHP教程全部内容,希望文章能够帮你解决PHP多线程的使用_PHP教程所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。