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) | 阅读(434067)
wedding crasher strain Email Homepage
2024-4-17 07:49
wedding crasher strainI found this blog site it’s very informative and helpful.
athirahaisi Homepage
2024-4-19 10:37
Dapatkan pengalaman bermain poker di website dewa poker dan poker 88 yang realistis dengan antarmuka yang intuitif dan grafis yang menarik. Nikmati nuansa kasino langsung dari kenyamanan rumah Anda.
athirahaisi Homepage
2024-4-19 10:37
Server kami menyediakan berbagai variasi permainan poker di website domino bet dan domino 88 online terbaik dan terpercaya, mulai dari Texas Hold'em hingga Omaha, sehingga Anda dapat memilih sesuai dengan preferensi Anda.
online dispensary Email Homepage
2024-4-30 09:17
online dispensaryAwesome! Really appreciate it, man.
Emwchinese Email Homepage
2024-5-6 14:49
选择一家优秀的留学生论文代写服务需要花费一些时间和精力,但这是值得的。通过选择一家可信赖的代写 http://www.emwchinese.com/ 服务,你可以为自己减轻论文写作的负担,同时确保获得一篇优质的论文,提升自己的学术水平。
Pineapple Punch Email Homepage
2024-5-16 08:16
<a href ="https://herbapproach.com/product/Pineapple-Punch/">Pineapple Punch</a> Awesome! Really appreciate it, man.
best indica Email Homepage
2024-5-21 18:47
I was really pleased with the content of your blog. It is really worth to read.best indica strains
cannabis recipes Email Homepage
2024-5-23 15:47
http://news.herbapproach.com/cannabis-recipes/Very interesting details, Keep it up!
canadian herbal bliss Email Homepage
2024-6-1 08:45
canadian herbal blissI found this blog site it’s very informative and helpful.
what is thca crystalline Email Homepage
2024-6-9 11:00
http://news.herbapproach.com/what-is-thca-crystalline-2/Very interesting details, thank you for sharing.
tangerine dream strain Email Homepage
2024-6-12 07:48
<a href =”https://herbapproach.org/product-category/concentrates/tangerine-dream-strain/”>tangerine dream strain</a>Awesome! Really appreciate it, man.
Pineapple Express Email Homepage
2024-6-13 08:52
Pineapple ExpressYour thoughts about this are very organized and amazing.
Sour Diesel Email Homepage
2024-6-13 10:47
I really like the knowledge you present here in this article. Keep it up!Sour Diesel
Bubba Kush Email Homepage
2024-6-14 13:54
Excellent! This is unpredictable!Bubba Kush
Gelato Homepage
2024-6-15 06:35
https://herbapproach.com/product/gelato-strain/Thank you for sharing this outstanding work. Good job!
shatter deals Canada Email Homepage
2024-6-30 10:08
http://herbapproach.com/product-category/concentrates-canada/shatter/ Thank you for sharing your knowledge, very precise information. Keep going!
tuna kush Email Homepage
2024-7-1 08:42
tuna kushI am impressed with your ideas. Sounds interesting!
montreal weed delivery Email Homepage
2024-7-3 07:40
montreal weed deliveryI found this blog site it’s very informative and helpful.
Pineapple Punch Email Homepage
2024-7-3 10:29
https://herbapproach.com/product/pineapple-punch/These ideaas are worth it!  appreciate all the efforts you put in!
Shinohara Tomoko
2024-7-9 20:00
Dapatkan keuntungan maksimal dari setiap taruhan Anda dengan mendaftar di website link alternatif dominobet kami yang sedang mengadakan promo khusus! Tunggu apa lagi? Ayo daftar sekarang di website domino88 online kami yang sedang mengadakan promo istimewa dan raih kemenangan besar!
分页: 13/15 第一页 上页 8 9 10 11 12 13 14 15 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]