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代码示例:
Jack
2020-7-7 14:53
I am happy to recommend this informative post with my college students who are taking essay writing help http://www.qualitydissertation.co.uk/essay-writing-help from me with error free work and minimum pass guarantee.
storiesig
2020-8-5 10:38
Greetings! Very useful advice in this particular article! It is the little changes that produce the biggest changes. Thanks a lot for sharing! Please check link storiesig
Nursing Healthcare Writing Services
2020-8-20 14:23
Nursing coursework writing service seekers have been on the rise lately since most learners need Nursing Heaithcare Writing Services, healthcare assignment writing help and nursing essay writing services.
Creative Writing Services
2020-10-14 13:20
Professional creative writing services are essential and they have become very popular for those in need of online creative writing assignment services since most of them seek Creative Writing Services. https://researchpapers247.com/creative-writing-service/
tertyyoiu
2020-10-21 14:00
Still looking for a way to get rid of the extra housing charges for your pet. If yes, then get your ESA letter for housing today and stop paying any charges for your Emotional Support Animal. https://fastesaletter.com/esa-letter-for-housing/
joker
2020-11-1 12:30
You write well I really like what you write. I would like to recommend my website. If it doesn't bother much, please It is the most modern casino and also has a 24-hour automatic deposit-withdrawal system if you are looking for an online casino website. This website is the number 1 site in the casino field, with many games for you to choose from <a href="https://jokerslot191.com/">jokerslot</a> <a href="https://pg-slot.net/">slot1234</a> <a href="http://sexygames66.net/">saxygame66</a>
joker
2020-11-1 12:30
You write well I really like what you write. I would like to recommend my website. If it doesn't bother much, please It is the most modern casino and also has a 24-hour automatic deposit-withdrawal system if you are looking for an online casino website. This website is the number 1 site in the casino field, with many games for you to choose from saxygame66 slot1234 jokerslot
Mellinda Andrews
2020-12-10 19:19
Being a personal Injury lawyer, it’s my responsibility to offer justice to the one who deserve. If you are injured severely after an accident, start filing a personal Injury lawsuit. You can visit our portal Personal Injury Attorney to know how to file a personal Injury lawsuithttps://www.personalinjuryattorneyfirm.com/
Personal Injury Attorney
2020-12-10 19:24
Being a personal Injury lawyer, it’s my responsibility to offer justice to the one who deserve. If you are injured severely after an accident, start filing a personal Injury lawsuit. You can visit our portal Personal Injury Attorney to know how to file a personal Injury lawsuit
Information Technology Essay Writing
2020-12-15 17:40
There are many information technology research writing services and Information Technology Essay Writing Services to choose from for those stuck with their information technology coursework writing services and information technology dissertation writing services.
dorcassmith
2021-1-12 13:30
Students find Legitimate Nursing Writing Services as being of great assistance since they are able to seek our nursing research paper writing services online and nursing essay writing services on time.
Jack
2021-2-3 19:42
I'm looking for this type of article for a long time and finally get it from here, I like this Pelle Pelle Jacket. Keep sharing this type of article.
sudoku 247
2021-2-20 19:23
thank you very much!sudoku 247
gerva
2021-3-12 19:33
Hardly matters if you are an expert of online cricket games. Betting in India or just a beginner. What matters is that you must owe the inside out knowledge of cricket online betting in India. It is only the basic concept that a bettor can consider while online betting but not all. You must know the game by its core if you wish to win the bets you place.<a href="https://gervaisjervis9.wixsite.com/tooltech/post/reasons-why-you-must-only-rely-on-license-cricket-betting-websites"...
James
2021-3-19 22:49
Greetings! Very useful advice in this particular article! It is the little changes that produce the biggest changes. Thanks a lot for sharing! Please check link betting in india
Jessica
2021-3-22 23:42
Sometimes, blogging is a bit tiresome specially if you need to update more topics. Also discover instagram story viewer to see instagram stories of public profiles
Jessica
2021-3-22 23:44
Sometimes, blogging is a bit tiresome specially if you need to update more topics. Also discover instagram story viewer to view instagram stories of public profilesinstagram story viewer
jessica
2021-3-22 23:44
Sometimes, blogging is a bit tiresome specially if you need to update more topics. Also discover instagram story viewer to view instagram stories of public profilesinstagram story viewer
jessica
2021-3-22 23:44
Sometimes, blogging is a bit tiresome specially if you need to update more topics. Also discover instagram story viewer to view instagram stories of public profilesinstagram story viewer
Aleena
2021-3-23 10:33
This is such a great post, and was thinking much the same myself. Another great update. If you are looking for a quality tour to Pakistan, visit Naran Kaghan Valley. So book your 3 Days Kaghan Valley now.
分页: 8/15 3 4 5 6 7 8 9 10 11 12