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) | 阅读(433055)
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
haleyhaleybrown Email Homepage
2022-7-1 19:18
It is a money processing service for internet companies, auction portals and many other commercial users............................... PayPal Contact Bellen
devinmarco565 Email Homepage
2022-8-1 19:45
Can You Have 2 Cash App Accounts- Avail quick fix from techiesCan You Have 2 Cash App Accounts? Are you searching for quick tech support on the cash app related concerns? When you find yourself stuck with technical problems, you must call the techies immediately. Just try to speak to the technical executives of the cash app team when you need quick help. To solve the problems of the cash app, you’ll be provided with a quick and possible solution. Users can dial the helpline number of the cash app to get rectification solutions. Also, read our blogs to get guidance. Can You Have 2 Cash App Accounts
devinmarco565 Email Homepage
2022-8-1 20:39
Are you stuck with the technical problems of the cash app? Don’t know about enabling the features of cash app borrow? How to borrow money from the cash app? Ring on time reachable phone number to reach experts and ask your queries directly from them. If you want an answer then you’ll have to dial the cash app helpline number. Techies will be available all day and night to assist you in solving woes. So, seek help from cash app technical specialists for fixing all your technical difficulties.How to borrow money from the cash app?
tinajose Email
2022-8-19 09:49
--> URL
jonemartin Email
2022-8-20 00:36
Create your very own company logo online, use our easy-to-use logo design tools to create your business logo, design your company logo, and create a business logo. Available here for Logo Design Dubai logodesigningdubai.ae
hioak简约导航网 Homepage
2022-8-21 22:32
推荐一个不错的实用工具导航网站,简约导航,包含各种常用网址导航,用户还不多,没有太多并发,长期运行就要考虑文中提到的性能考虑了
GB Whatsapp Email Homepage
2022-8-29 01:07
You can download GBWhatsapp from our website and enjoy all the amazing features of it.GB Whatsapp
먹튀타이거 먹튀검증 Email Homepage
2022-9-14 11:33
먹튀타이거 먹튀검증먹튀타이거 먹튀검증 If you have any questions about the safe eat-and-run guarantee company provided by Eat Tiger, please contact the customer center at any time!
Carlisle Email
2022-9-19 10:09
If you are on the hunt for a company that not only cares for its clients but also the city in which it operates, then you should call NYC Demolition Contractor Co today! Learn More.
moversinkondapur Email Homepage
2022-10-12 16:08
Packers and movers in kondapur are professionals who can help you with all your needs - loading and unloading of goods, packing and unpacking of goods, transportation of goods, storage as well as unpacking.https://packersandmoversinkondapur.com/
ao whatsapp Email Homepage
2022-10-13 19:19
The biggest advantage of ao whatsapp download is that we can install additional functions from its settings without the need to download additional APK files.https://gbappsx.com/aowhatsapp-apk/
分页: 10/15 第一页 上页 5 6 7 8 9 10 11 12 13 14 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]