PHP 真正多线程的使用

| |
[不指定 2013-12-17 11:17 | by 张宴 ]
  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 是必选项:
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"


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





技术大类 » PHP/JS/Shell | 评论(289) | 引用(0) | 阅读(434048)
Bungie Email Homepage
2021-3-24 20:01
Live betting on cricket or any other sport involves betting on outcomes in a game or tournament after it started. This helps increase the excitement of watching cricket since you’ll be able to find new elements and results to be excited about.<a href="http://resurrection.bungie.org/forum/index.pl?profile=john+wick">http://resurrection.bungie.org/forum/index.pl?p...
Manoj Email Homepage
2021-3-25 20:21
I would like to convey my admiration for your generosity in support of men and women that have the need for help with this particular concern. Your special dedication to getting the message all over had been wonderfully productive and have all the time made professionals much like me to attain their dreams. Your own invaluable tutorial means a great deal to me and additionally to my office workers. Thank you; from everyone of us. Also visit online cricket betting sites in india
Manoj Email Homepage
2021-3-25 20:24
You completed a number of nice points there. I did a search on the issue and found nearly all people will have the same opinion with your blog. Also visit online cricket betting sites in india and start betting online cricket betting sites
Manoj Email Homepage
2021-3-25 20:24
You completed a number of nice points there. I did a search on the issue and found nearly all people will have the same opinion with your blog. Also visit online cricket betting sites in india and start bettingonline cricket betting sites
Madiha Email Homepage
2021-3-30 20:36
Advantageously, typically the submit is really the very best about this laudable theme. To be sure with all your a conclusion and will thirstily await the following revisions. Really stating cheers won’t only end up being suitable, to your good readability within your creating. I may at once seize a person’s rss to sleep in abreast of virtually any upgrades. Fine job and much success within your organization business! Visit cricket betting sites list for online cricket betting in India
Madiha Email Homepage
2021-3-30 20:38
Advantageously, typically the submit is really the very best about this laudable theme. To be sure with all your a conclusion and will thirstily await the following revisions. Really stating cheers won’t only end up being suitable, to your good readability within your creating. I may at once seize a person’s rss to sleep in abreast of virtually any upgrades. Fine job and much success within your organization business! Visit cricket betting sites list for online cricket betting in India
Madiha Email Homepage
2021-3-30 20:38
Advantageously, typically the submit is really the very best about this laudable theme. To be sure with all your a conclusion and will thirstily await the following revisions. Really stating cheers won’t only end up being suitable, to your good readability within your creating. I may at once seize a person’s rss to sleep in abreast of virtually any upgrades. Fine job and much success within your organization business! Visit cricket betting sites list for online cricket betting in India
Madiha Email Homepage
2021-3-30 20:38
Advantageously, typically the submit is really the very best about this laudable theme. To be sure with all your a conclusion and will thirstily await the following revisions. Really stating cheers won’t only end up being suitable, to your good readability within your creating. I may at once seize a person’s rss to sleep in abreast of virtually any upgrades. Fine job and much success within your organization business! Visit cricket betting sites list for online cricket betting in India
Madila Email Homepage
2021-3-30 20:40
Advantageously, typically the submit is really the very best about this laudable theme. To be sure with all your a conclusion and will thirstily await the following revisions. Really stating cheers won’t only end up being suitable, to your good readability within your creating. I may at once seize a person’s rss to sleep in abreast of virtually any upgrades. Fine job and much success within your organization business! Visit cricket betting sites list for online cricket betting in India
issa Email Homepage
2021-3-31 13:44
Interesting   read , I am  going to spend more time  learning about this subject. Also explore best cricket betting sites on cricket betting india and start online betting in india.
Vingle Email Homepage
2021-4-20 21:04
Cricket betting india will help you choose one of the best online cricket betting websites by supplying you with truthful and impartial feedback as well as the most up-to-date details about how to bet on cricket.<a href="https://www.vingle.net/CricketBetting">https://www.vingle.net/CricketBetting</a>
Godotengine Email Homepage
2021-4-30 23:52
If you want to learn how to place cricket bets? Cricket betting india is a website dedicated to providing you with the most up-to-date material on cricket betting guides, terminology, and online betting in India. We compile online cricket betting guides in India.<a href="https://godotengine.org/qa/user/cricketbetting">https://godotengine.org/qa/user/cricketbetting</a>
narantour Email Homepage
2021-5-4 00:54
I became acquainted with what I was normally interested in thanks to you. &quot; I am overjoyed with the high quality of the posting. I wish you a pleasant day today. clickherehttps://www.alora.io/forums/user/536864-narantravel/
prekhshasharma Email Homepage
2021-6-26 12:00
Get Packers and Movers Kolkata/ List of Top Reliable, 100% Affordable, Verified and Secured Service Provider. Get Free ###Packers and Movers Kolkata Price Quotation instantly and Save Cost and Time. ✔✔✔Packers and Movers Kolkata Reviews and Compare Charges for household Shifting, Home/Office Relocation, ***Car Transportation, Pet Relocation, Bike SHifting @ https://kolkatapackersmovers.in/
Elli Gibson Email
2021-8-9 12:28
Creativity and the free exchange of ideas can only expand through research. Thanks for this section and we can always express freely on the thoughts or opinion that we may suggest. But in here, i would really like to discuss more. Specially about my project. Hope you can notice this message. Thanks  เทคนิคเล่นบาคาร่า
shearlingstore Email Homepage
2021-8-9 19:43
Thanks for share this informative post.Buy Our huge choice of Real Leather Jackets, Classic B-3 Sheepskin Leather Bomber Jacket, Motorcycle Riding Suits, Motogp Leather Suits, Harley Davidson Motorcycle Jackets and Motorcycle Riding Jackets from Shearling Leather Shop with discounted price & worldwide free shipping.Real Leather Jackets
Wordpress Email Homepage
2021-8-17 19:08
Anal play gives a great feeling of pleasure to both men and women. Anal toys are designed to trigger the soft region of anus during anal sex. Toys range from balls to erectile elements that stimulate the areas inside anus.https://sextoysonlineaustralia.wordpress.com/2020/09/22/anal-sex-toys/https://sextoysonlineaustralia.wordpress.com/2020/09/22/anal-sex-toys/
swgmortgages Email Homepage
2021-8-19 19:42
Slight variations in rates have a significant impact on your repayment rate, so it is essential to seek the advice of a mortgage broker you trust. You are probably looking for remortgaging to get funding for home renovation, remortgaging, or just finding a mortgage with a better interest rate.https://www.teachertube.com/user/channel/swgmortgageshttps://www.teachertube.com/user/channel/swgmortgages
jayson Email Homepage
2021-8-24 18:28
This is fascinating; you're a terrific writer. I've subscribed to your feed and look forward to reading more of your amazing works. Thank you for sharing your experience with us; your blogs are usually informative. Thank you for your fantastic blog. https://list.ly/marketing4service/activity
SallieStevens Email
2021-10-29 17:35
Backlinks are links from a page on one website to another. This is important to us and your are giving us a chance and opportunity to help each other. We are hoping to have same goal the future of ours. https://www.chokdeebacarrat.com/ole777/
分页: 9/15 第一页 上页 4 5 6 7 8 9 10 11 12 13 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]