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 | 评论(295) | 引用(0) | 阅读(435006)
Weed measurements Email Homepage
2024-10-25 04:33
https://news.herbapproach.com/weed-measurements-your-ultimate-guide/Interesting blog! It provided us with valuable information. Keep up the good work!
zigzag rolling papers Email Homepage
2024-11-2 06:13
https://herbapproach.com/product-category/zigzag-rolling-papers/Thanks, you have made a wonderful post. I love and appreciate your commitment.
Carl Email
2024-11-10 04:21
카지노사이트 먹튀 검증 사이트, 카지노사이트 추천, 안전한 카지노사이트, 바카라사이트, 슬롯사이트, 보증업체 추천 및 각종 이벤트 정보https://calorierestriction.org
dab rigs for sale Email Homepage
2024-11-10 16:34
https://herbapproach.com/product-category/accessories/dab-rigs-canada/Thanks for sharing this important info from your website.
bulk distillate canada Email Homepage
2024-11-10 21:35
https://herbapproach.com/product-category/concentrates/bulk-distillate-canada/I found this blog site it’s very informative and helpful.
710 weed meaning Email Homepage
2024-12-4 21:22
<a href =”https://herbapproach.com/product-category/concentrates/710-weed-meaning/”>710 weed meaning</a>Thanks, you have made a wonderful post. I love and appreciate your commitment.
cheapwee Email Homepage
2024-12-14 14:47
https://herbapproach.com/product-category/concentrates/cheapwee/I found this blog site it’s very informative and helpful.
thc syrup dispensary Email Homepage
2024-12-21 16:39
Your work is going to leave a lasting impression on us. Nice one!thc syrup dispensary
sleep pen Email Homepage
2024-12-21 16:39
You always have the best input in your blog. Fantastic!sleep pen
buy shatter Email Homepage
2025-1-5 20:32
<a href =”https://herbapproach.com/product-category/concentrates/buy-shatter/”>buy shatter</a>Thanks, you have made a wonderful post. I love and appreciate your commitment.
bubba cookies Email Homepage
2025-1-10 06:17
https://herbapproach.com/product/bubba-cookies/The posts are very brief. It’s really excellent, keep it up!
irlanyuniara Email
2025-1-13 20:14
Game dengan faktor keberuntungan 100% di link dewa poker dan poker 88 adalah tempat yang tepat untuk mendapatkan hiburan dan kesenangan tanpa batas. Saya sangat menikmati setiap sesi permainan dan hadiah yang berhasil saya raih.
yuniara Email
2025-1-13 20:15
Saya sangat terkesan dengan kualitas game dengan faktor keberuntungan 100% di link domino bet dan dewa gg indonesia. Tidak hanya menyenangkan, tapi juga memberikan peluang besar untuk mendapatkan jackpot yang menggiurkan.
thc vape pen canada Email Homepage
2025-1-14 18:50
https://herbapproach.com/product-category/best-extracts-online-canada/vape-pen/Highly appreciate you for covering the best info in this blog. Great job!
best extracts online canada Email Homepage
2025-1-21 08:36
<a href =”https://herbapproach.com/product-category/herbapproach-com/best-extracts-online-canada/”>best extracts online canada</a>I found this blog site it’s very informative and helpful.
分页: 15/15 第一页 上页 10 11 12 13 14 15 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]