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 | 评论(446) | 引用(0) | 阅读(462876)
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
分页: 23/23 第一页 上页 18 19 20 21 22 23 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]