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 | 评论(285) | 引用(0) | 阅读(433168)
토토사이트추천 Email Homepage
2023-8-28 08:51
The beautiful things you have been sharing with us. Let's continue writing this type of post. The time wasted on the road for tuition fees can now be used for studying.토토사이트추천
토토사이트 Email Homepage
2023-9-8 09:25
Undoubtedly, this is really great. All these smaller facts were created using digital and credential technology. I actually like the best value of reasoning.토토사이트
토토사이트 Email Homepage
2023-9-8 09:26
Undoubtedly, this is really great. All these smaller facts were created using digital and credential technology. I actually like the best value of reasoning.토토사이트
kiriazicompany Email Homepage
2023-9-20 13:50
<a href="https://twkel.com">رقم توكيل اجهزة </a><a href="https://sharpfixe.com"> صيانة شارب للثلاجات </a><a href="https://toshibae.com"> صيانة توشيبا مصر</a><a href="https://lg-maintenanc.com/"> صيانة ال جي مصر</a><a href="https://kiriazicompany.com/"> صيانة كريازي مصر</a>
Angel17 Email
2023-9-24 15:14
What an interesting post. Thank you for this one. visit us online
메이저사이트 Email Homepage
2023-10-4 17:32
It is because I have read your article that I am able to improve my skills and ideas in this field. Thank you very much.메이저사이트
aaareplicatrade Email Homepage
2023-11-7 20:43
This particular is usually apparently essential and moreover outstanding truth along with for sure fair-minded and moreover admittedly useful My business is looking to find in advance designed for this specific useful stuffs…  replica clothes
bennypal Email Homepage
2023-12-20 22:01
LPB Piso Wifi admin portal allows you to Insert Money, Pause Time, and change various settings for vendo owners and customers at http://10.0.0.1.https://10001pisowifipausetime.com/https://10001pisowifipausetime.com/
1688goatbet Email Homepage
2024-1-18 07:02
1688GOATBET  <a href="https://1688goatbet.co/" rel="nofollow ugc"> goatbet </a>  เว็บแทงบอลออนไลน์ คาสิโนออนไลน์ สล็อตเว็บตรงทรูวอเลท goatbet ที่คุณรุ่นใหม่เลือก ด้วยระบบ สล็อต1688 เว็บแทงบอลออนไลน์ ที่ดีที่สุด ฝาก-ถอน อัตโนมัติ รวดเร็วทันใจ ปลอดภัย 100% ทาง goatbet
Angel17 Email
2024-2-4 11:08
This is nice. Thanks for sharing this.   Matt Lengacher
Lucky cola
2024-2-7 16:38
Defeat your opponents and claim your gaming glory.  Lucky cola
sandeep Email Homepage
2024-2-10 17:38
A profit-goal-calculator tool assists businesses in setting and achieving financial objectives by calculating the revenue needed to reach a specified profit target. By inputting costs, desired profit margin, and other financial metrics, users can determine revenue goals and develop strategies for sustainable growth and profitability.<a href='https://calculatoruniverse.com/profit-goal-calculator/">https://calculatoruniverse.com/profit-goal-calculator/&l...
AnitaLaksita Email
2024-2-22 16:08
Tunggu apa lagi? Mari kita bergabung di dominobet dan nikmati keseruan bermain poker online. Temukan pengalaman bermain yang tak terlupakan, hadapi pemain-pemain handal, dan raih kemenangan besar bersama komunitas kami. Ayo, jangan lewatkan kesempatan ini! Daftar sekarang di dominobet  dan buktikan kehebatanmu di meja hijau!
DanielP Email
2024-2-23 15:34
Maximize your business potential with our profit calculator tool, swiftly analyzing margins and projections for informed decision-making. Simplify financial planning and strategy implementation with our intuitive profit calculator, empowering you to optimize your bottom line.https://calculatoruniverse.com/profit-calculator/
Downloadgbapk Email
2024-2-24 17:38
Some time returned it was recommended that you can force the working system to load every other example of the personal home page executable and handle different simultaneous approaches. Its excellent. But You can also read about GBWhatsapp application.
online Email Homepage
2024-2-28 19:57
Thank you for sharing the good writes up. Amazing!online dispensary british columbia
tuna weed Email Homepage
2024-3-15 07:40
https://herbapproach.org/product-category/concentrates/tuna-weed/I found this blog site it’s very informative and helpful.
henryjones116 Email Homepage
2024-3-19 14:47
As someone juggling multiple commitments, I often found myself struggling to balance academic responsibilities with other obligations. Programming Assignment Helper came to my rescue with their expert assistance, allowing me to focus on mastering programming concepts without the added stress of looming deadlines. Their professionalism and dedication have been instrumental in my academic success.Programming Assignment Helper
most expensive bong Email Homepage
2024-3-28 17:02
https://herbapproach.org/product-category/most-expensive-bong/I found this blog site it’s very informative and helpful.
AK 47 Email Homepage
2024-4-4 05:37
I wanted to congratulate you on your determination in making this blog. Keep up the great work.AK 47
分页: 12/15 第一页 上页 7 8 9 10 11 12 13 14 15 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]