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 | 评论(493) | 引用(0) | 阅读(464315)
raza Email
2026-10-21 18:18
Sustain the nice do the job, When i understand several threads within this web page in addition to I'm sure that a world-wide-web blog site is usually authentic useful possesses bought communities connected with excellent facts.  slot gacor
MTS Email
2026-10-21 20:26
We notion it really is a good idea to publish could anyone was initially having issues searching for however , My organization is a bit of dubious just have always been allowed to insert leaders together with contact regarding at this point.  Dispensary near me now
wsk Email
2026-10-22 17:11
Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained!  seng toto
raza Email
2026-10-22 17:41
Sustain the nice do the job, When i understand several threads within this web page in addition to I'm sure that a world-wide-web blog site is usually authentic useful possesses bought communities connected with excellent facts.  situs togel
YSX Email
2026-10-22 21:35
It's which means that amazing together with inspiring. We really enjoy any designs together with whoever should get it all during the post shall be grinning.  olxtoto macau  I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.  slot777  Thanks for your time designed for advertisment a great very good guide! I noticed your internet-site ideal for a must have. It has splendid and even effective content articles. Compete the best get the job done!  olxtoto daftar
hasnain ali Email
2026-10-22 22:27
This is certainly hence attractive plus artistic. I like a colorations plus whichever company may get them while in the mailbox might be smiling.  olxtoto slot
wsk Email
2026-10-24 15:23
This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free.  sengtoto togel
5z Email
2026-10-24 16:01
thanks this is good blog.  miototo togel  thanks this is good blog.  pestoto link alternatif  thanks this is good blog.  togel online
wsk Email
2026-10-24 18:02
New web site is looking good. Thanks for the great effort.  Alexistogel
sweg Email
2026-10-24 21:46
I did so love looking through reports created on this site. They can be striking and has now loads of handy information and facts.  Buy weed Canada
hasnain ali Email
2026-10-26 18:47
Freezing consideration perhaps it is an understanding to create could possibly help everybody else is having troubles searching though My group is some unclear a lot more i am permitted to get details and even communications information at these.  Mercure Bangkok
SEO Email Homepage
2026-10-27 14:11
Interesting topic for a blog. I have been searching the Internet for fun and came upon your website. Fabulous post. Thanks a ton for sharing your knowledge! It is great to see that some people still put in an effort into managing their websites. I'll be sure to check back again real soon.  order flow
tasz Email
2026-10-27 16:22
When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style.  레플리카
mts Email
2026-10-27 21:52
Thank you for taking the time to publish this information very useful!  종합 심리검사
SAMEER SEO Email
2026-10-28 20:39
it's truly wonderful weblog. it's realy educational along with a this type of great work. i really like this particular.  link alternatif olxtoto
wsk Email
2026-10-29 15:27
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info.  toto macau
taha Email
2026-10-29 20:32
We arrived on your weblog whilst concentrating simply somewhat submits. Good technique for following, I'll be book-marking at the same time grab your own total increases.  quelle est le code promo 1xbet
raza Email
2026-10-31 13:39
The on-line world might be bogged downwards with the help of counterfeit web logs without a proper personal message nonetheless put up was basically awesome not to mention value typically the read through. Regards for the purpose of showing this unique when camping.  할인코드
raza Email
2026-10-31 18:26
The on-line world might be bogged downwards with the help of counterfeit web logs without a proper personal message nonetheless put up was basically awesome not to mention value typically the read through. Regards for the purpose of showing this unique when camping.  반가상 업체
wsk Email
2026-10-31 19:08
Any movies is normally outstanding. You've gotten a lot of particularly as good writers and singers. Document prefer you will the best quality about financial success.  성피 임대
分页: 23/25 第一页 上页 18 19 20 21 22 23 24 25 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]