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) | 阅读(434070)
LA Confidential Email Homepage
2024-7-17 08:39
https://herbapproach.org/product/la-confidential/ Your writing has a magical quality that transports me to another world.
what is the endocannabinoid system Email Homepage
2024-7-19 07:34
http://news.herbapproach.com/what-is-the-endocannabinoid-system-the-full-guide/Very interesting details, Keep it up!
buy online weeds Email Homepage
2024-7-19 07:59
buy online weedsAwesome! Really appreciate it, man.
11 hydroxy metabolite Email Homepage
2024-7-19 08:10
http://news.herbapproach.com/11-hydroxy-metabolite/Very interesting details, Keep it up!
flyte vape pen Email Homepage
2024-8-6 06:06
https://herbapproach.com/product-category/vendors/flyte-concentrates/ Thank you for sharing interesting details. Keep it up!
100mg of thc Email Homepage
2024-8-6 09:11
<a href =”https://herbapproach.org/product-category/flowers/100mg-of-thc/”>100mg of thc</a>I found this blog site it’s very informative and helpful.
sfv og kush Email Homepage
2024-8-18 04:44
https://herbapproach.com/product/sfv-og-kush-strain/Looking forward for this wonderful and meaningful blogs.
fulfillment Email Homepage
2024-8-23 08:41
https://breadstack.com/ecommerce-operations/fulfillment/Good article and it’s very inspiring. Keep going!
cali bubba kush Email Homepage
2024-8-27 01:21
https://herbapproach.org/product-category/cannabis-flower/cali-bubba-kush/Thanks for the useful article
online dispensary prince edward island Email Homepage
2024-8-30 14:49
<a href =”https://herbapproach.com/product-category/cannabis-flower/online-dispensary-prince-edward island/”>online dispensary prince edward island</a>Thanks for sharing such a informative content.
canadian edibles companies Email Homepage
2024-9-5 09:37
<a href="https://herbapproach.com/product-category/edibles/”>canadian edibles companies</a>Sending appreciation to your blog. Very Informative!
g 13 strain Email Homepage
2024-9-5 16:40
g 13 strainThanks, you have made a wonderful post. I love and appreciate your commitment.
online dispensaries Email Homepage
2024-9-6 06:27
https://herbapproach.com/Thanks for sharing this content, amazing post. Keep it up!
kush cakes strain Email Homepage
2024-9-9 16:51
<a href =”https://herbapproach.org/product-category/concentrates/kush-cakes-strain/”>skuh cakes strain</a>Great post! Very insightful and helpful tips!
Itoh Shuji
2024-9-14 02:55
Temukan kesenangan tak terbatas dan hadiah-hadiah menarik di website dominobet kami yang sedang menggelar promo spesial! Jadi bagian dari petualangan slot online yang mendebarkan dengan mendaftar di website login dewapoker kami yang menyelenggarakan promo khusus untuk Anda!
100mg of thc Email Homepage
2024-9-19 10:23
<a href =”https://herbapproach.org/product-category/flowers/100mg-of-thc/”>100mg of thc</a>Thanks for the useful article
unicorn poop weed Email Homepage
2024-9-28 09:57
<a href =”https://herbapproach.org/product-category/concentrates/unicorn-poop-weed/”>unicorn poop weed</a>I found this blog site it’s very informative and helpful.
twisted extracts sativa Email Homepage
2024-10-14 06:09
<a href =”https://herbapproach.org/product-category/concentrates/twisted-extracts-sativa/”>twisted extracts sativa</a>I found this blog site it’s very informative and helpful.
cannabis store thunder bay Email Homepage
2024-10-21 03:25
https://herbapproach.com/product-category/flowers/online-dispensary/Awesome! Really appreciate it, man.
craft weed canada Email Homepage
2024-10-23 12:05
https://news.herbapproach.com/craft-weed-canada/I found this blog site it’s very informative and helpful.
分页: 14/15 第一页 上页 9 10 11 12 13 14 15 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]