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 | 评论(189) | 引用(0) | 阅读(396018)
hoobs Email Homepage
2022-1-4 13:04
A very excellent blog. I am thankful for your blog post. I have found a lot of approaches after visiting your post. I am hoping for the same best work from you in the future as well. I wanted to thank you for this website! Thanks for sharing. Great websites! <a href="https://gbplusapps.com/og-whatsapp-apk-download/">og whatsapp</a>
hoobs Email Homepage
2022-1-4 13:08
A very excellent blog post. I am thankful for your blog post. I have found a lot of approaches after visiting your post. Thanks a lot for sharing! Please check link https://gbplusapps.com/og-whatsapp-apk-download/
메이저놀이터 Email Homepage
2022-4-17 10:42
There are also articles on these topics on my blog   and I hope you visit once and have a deep discussion!  메이저놀이터
asimhameedimd Email Homepage
2022-4-19 03:34
My name is Asim Hameedi and I am a board-certified best cardiologist in New York, Practicing non-invasive and interventional cardiology since 2003. My undergraduate degree is from St. John’s University, Queens, NY. Doctor of Medicine is from SUNY Downstate Health Science Center, Brooklyn, NY.
painters and decorators in dubai Email Homepage
2022-5-1 18:31
I have really enjoyed reading your blog posts. Thank you for sharing this amazing information with us.
Painting Services in Qatar Email Homepage
2022-5-1 18:38
Thanks for sharing this with so much of detailed information, its much more to learn from your article. Keep sharing such good stuff.
Mens Brown Cafe Racer Leather Jacket Email Homepage
2022-5-9 15:22
Pretty nice post. I just stumbled upon your weblog and wanted to say that I have really enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again soon! Mens Brown Cafe Racer Leather Jacket
things to do Email Homepage
2022-6-2 00:34
Travelling is a good way to relax when you are under pressure at works. But finding some beautiful places is not really easy. Therefore, things to do provides many reviews from people who has gone it to help you choose your destination.https://www.thingstodopost.org
分页: 10/10 第一页 上页 5 6 7 8 9 10 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]