PHP 5.3 以上版本,使用pthreads PHP扩展,可以使PHP真正地支持多线程。多线程在处理重复性的循环任务,能够大大缩短程序执行时间。
我之前的文章中说过,大多数网站的性能瓶颈不在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
1、扩展的编译安装(Linux),编辑参数 --enable-maintainer-zts 是必选项:
添加:
2、给出一段PHP多线程、与For循环,抓取百度搜索页面的PHP代码示例:
我之前的文章中说过,大多数网站的性能瓶颈不在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
1、扩展的编译安装(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
./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"
2、给出一段PHP多线程、与For循环,抓取百度搜索页面的PHP代码示例:
re
2016-11-7 12:01
文字[i][u][/u][bfsfsdfsdfds][/b][/b][b][/i][i[u]][/i][/u]
re
2016-11-7 12:01
[b]fsdfs[/b]
12312
2017-1-2 15:10
你好,extends Thread 的Thread是怎么样的?
王
2017-2-22 10:18
不知道curl并发和多线程,谁快?
久久SEO
2018-1-15 11:38
本地测试用了这个pthreads,感觉还是不错的.线上就算了吧,我的网站 久久SEO 欢迎访问
王王
2018-8-31 10:52
extends Thread 的Thread是怎么样的?gmail sign up
wings io
2018-11-26 18:01
PHP 5.3 is very good, with many nice features. I like so much.wings io
mi ka
2018-12-4 16:39
To have a new article, meaning you have to learn and try a lot in the search, I am very impressed with your article. temple run
丹恪梦
2018-12-25 09:48
长见识了
virat
2019-6-29 13:12
The dynamism of the Power combined together of a core focused high-spirited,aggressive and enthusiastic team at Bangalore Secretarial Services with an bulls eye view of the envisioned target is success Mantra in Bangalore Secretarial Services and the vitality of the Power combined together of focused high-spirited individuals show cases the buoyancy and ebullience in our set goals and is the success Mantra in Bangalore Secretarial Serviceshttps://www.bangaloresecretary.com/placements-consultancy-services
virat
2019-6-29 13:12
we Headhunters in India know precisely where to look for your desired person and existing clients with us know that we are the only the most convenient and straight forward head hunting firm in India and user-friendly and time-effective way to fill a role. https://www.bangaloresecretary.com/head-hunters-in-India
mira
2019-9-24 17:22
univers med , jazz chirurgie esthetique
killua-x
2019-10-3 20:12
学习了
Research Papers for Sale
2019-10-30 15:47
Getting Nursing Writing Services from experienced writers provides you with more free time to focus on other tasks. The company have Custom College Papers who offer Cheap Paper Writing and are knowledgeable with Chicago, MLA, and APA style.https://www.meldaresearch.com/online-research-papers-for-sale/
Research Papers Writing Services
2019-11-8 21:03
To further enhance cost-effectiveness of Custom Research Paper Services, there is a revision policy for the Custom Research Paper Writing Services where essayist review the task, despite the period that has elapsed since we presented your Legitimate Custom Research Paper assignment.https://www.meldaresearch.com/research-papers-writing-service-with-experienced-writers/
xd
2019-11-9 16:34
电商专用快递网站www.dh5u.com单号无忧
Best College Research Papers Writers
2019-11-19 19:34
It is understandable that one is more confident when their task is in the hands of the Order College Research Papers Expert than a novice; thus, one hires Custom College Research Papers Writer who delivers the ideal Legitimate College Research Paper.
meldaresearch
2019-11-20 14:28
Legitimate Custom Assignment Writing Provider are not that expensive for Custom Assignment Writing Services since they complete Award Winning Assignment Writing Services?
Custom Book Review Services
2019-11-26 16:25
Unsure which firm produces Best Book Review Writing Services in the industry to solve your Custom Book Review Services? Legitimate Book Review Writing Company is there for all your book review needs.https://researchpapers247.com/book-review-2/
Maxwell
2019-11-27 16:01
写的很棒.spanish dictionary
分页: 6/15 1 2 3 4 5 6 7 8 9 10