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) | 阅读(464309)
SAMEER SEO Email
2026-10-31 20:34
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.  AK 게임
hasnain ali Email
2026-10-31 21:18
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.  AK 게임
sei
2026-10-31 21:36
As i followed on your web log even while aligning really just a little submits. Attractive technique for then, We are book-marking at one time seize ones own whole goes up.  바둑이사이트
raza Email
2026-11-1 13:27
My spouse and i came up on your web site even though paying attention only a little submits. Wonderful technique for up coming, We are book-marking immediately use your current comprehensive soars.  가상계좌 솔루션
mts Email
2026-11-1 15:39
Thank you for taking the time to publish this information very useful!  홀덤사이트
sd Email Homepage
2026-11-1 16:50
Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work.  섯다사이트
wsks Email
2026-11-1 17:20
Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work.  온라인맞고
hasnain ali Email
2026-11-2 14:24
Thanks with regard to publishing this type of excellent post! I discovered your site ideal for my personal requirements. It has fantastic as well as useful articles. Continue the great function!  반가상 솔루션
wsk Email
2026-11-2 15:05
Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post.  맞고사이트
SAMEER SEO Email
2026-11-2 16:06
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.  서귀포 성인 피시방
asd Email Homepage
2026-11-2 21:26
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.  sexy secret santa gifts
ad Email Homepage
2026-11-3 00:18
You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers.  olxtoto resmi
SEO Email Homepage
2026-11-3 17:01
Nice post. I was checking constantly this blog and I am impressed! Extremely helpful information specially the last part I care for such info a lot. I was seeking this particular information for a very long time. Thank you and good luck.  slot depo 5k
SAMEER SEO Email
2026-11-3 20:01
Fine place, a an exceptionally amazing webpage that there is at this point, stick to beneficial financial job, shall be to come back.  안성 성인 피시방
raza Email
2026-11-4 19:45
This is the wonderful content, Cheers pertaining to supplying us this info. Preserve putting up.  バイナリーオプション
SAMEER SEO Email
2026-11-4 20:08
Thank you so much meant for ad an extremely terrific report! I recently found your blog post perfect for my best necessities. It contains terrific together with practical articles or blog posts. Stick to beneficial financial job!  부동산변호사
se
2026-11-4 20:55
We've just lately commenced a new web site, the details anyone present on this internet site features made it easier for us drastically. Cheers pertaining to your occasion & operate.  ผลบอลสด thscore 3
rs Email
2026-11-4 22:00
Your current tunes can be remarkable. You've got a number of quite accomplished musicians. My spouse and i would like anyone the top involving good results.  cow print thongs
mts Email
2026-11-7 18:41
Attractive blog post, the nation's a truly nice web log that you have got these, compete the best get the job done, can be once again.  FTMO passing service
asd Email Homepage
2026-11-8 15:56
Really unquie place, Thank you so much meant for posting The experience. Fantasticly penned report, anxieties most of folks given similar volume of material as you may, the online market place might be a a lot better put. Be sure to keep it up!  toto macau
分页: 24/25 第一页 上页 19 20 21 22 23 24 25 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]