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) | 阅读(433132)
Affordable Research Paper Writing Services Homepage
2019-12-12 21:53
An exceptional Online Custom Research Paper Services will ensure that the client receives high quality Non-Plagiarized Custom Research Paper and College Custom Research Paper Help Services.
Custom Assignment Writing Help Services Email Homepage
2019-12-16 13:43
Our custom writing team work collectively to complete Custom Assignment Writing Services as they offer the best College Assignment Writing Services and Online Assignment Writing Help.https://researchpapers247.com/assignment-writing-services-2/
Best Custom Essay Writing Services Email Homepage
2019-12-16 19:31
Online Essay Writer Services are well known in providing Online Custom Essay Writing Services and any other Legitimate Online Writing Services needs.
Custom Coursework Writing Services Email Homepage
2019-12-18 17:01
robert_nukk Email Homepage
2020-1-8 18:54
Tadalista 10 Mg Want to hardcore experience without early discharge? buy Tadalista 10 which is the best ED treatment in the USA. Check out Price, Reviews & Side Effectshttps://www.flatmeds.com/product/tadalista-10-mg/
Physical Education Assignment Help Services Homepage
2020-1-9 20:04
Students have the advantage of selecting the best  Physical Education Assignment Writing Services since there are qualified writers for Physical Education Essay Writing Services and Physical Education Research Paper Writing Services.https://researchpapers247.com/
Befabulla Email
2020-1-21 14:32
Buy Men and Women Clothing Shop Online from trendy apparel for women & men at best prices. Select your favorite clothing from the fashionable collection on BeFabullahttps://www.befabulla.com/
Befabulla Email
2020-1-21 14:35
Buy Men and Women Clothing Shop Online from trendy apparel for women & men at best prices. Select your favorite clothing from the fashionable collection on BeFabullahttps://www.befabulla.com/
Topessaywriting Homepage
2020-1-27 12:51
Any student who may need Legitimate Research Paper Writing Service is free to request our writers for assistance from TopEssayWriting</a> as they are committed to helping clients when they visit TopEssayWriting Login. https://topessaywriting.com/
Nursing Research Papers Email Homepage
2020-2-10 14:55
When students seek our Nursing Research Paper Writing Services from us, they are assured to receive the best Custom Nursing Writing Services that meets all their writing needs and Professional Nursing Writing Services one that has been written following all the instructions.
Nursing Research Papers Email Homepage
2020-2-11 21:46
When students seek our Nursing Research Paper Writing Services from us, they are assured to receive the best Custom Nursing Writing Services that meets all their writing needs and Professional Nursing Writing Services one that has been written following all the instructions.
Useless Websites Email Homepage
2020-2-24 12:22
Useless Websites https://uselesslyweb.com/ Useless Websites Useless Websites
Economics Writing Services Email Homepage
2020-3-11 16:03
Through our experts, we offer all kinds of Economics Essay Writing Services and Economics Research Paper Writing Services to suit the needs of every student when they are given any Economics Coursework Writing Services.
23isback Homepage
2020-4-12 21:09
Miyo Tashima
2020-4-22 21:02
好きなホームエンターテイメントは何ですか? 私は個人的にオンラインカジノ スロットが本当に好きです。 これはあなたの運をテストし、本当のお金を稼ぐための素晴らしい機会です。
Information Technology Writing Services Email Homepage
2020-5-11 16:42
There are so many online information technology writing services that are being offered for students to seek information technology research paper services and information technology essay writing services.  information technology research paper services
zyan Email Homepage
2020-6-19 10:03
When students seek our dog water fountain  Nursing Research Paper Writing Services from us, they are assured to receive the best electric flea comb  Custom Nursing Writing Services that meets all solar fountain their writing needs and Professional Nursing Writing Services one dog toothbrush  that has been  written following all the instructions.
zyan Email Homepage
2020-6-19 10:04
Through our experts, we offer all kinds of color changing light bulb  Economics Essay Writing Services and leg pillow Economics Research Paper flea and tick collar  Writing Services to suit the needs of every student when they are given any electric knife sharpener  Economics Coursework Writing Services.
zyan Email Homepage
2020-6-19 10:04
History Research Paper Services Email Homepage
2020-6-19 19:34
Professional History Research Paper Services is very popular for students in search for history essay writing help services and history assignment writing services.History Research Paper Services
分页: 7/15 第一页 上页 2 3 4 5 6 7 8 9 10 11 下页 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]