<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[张宴的博客]]></title> 
<link>http://zyan.cc/index.php</link> 
<description><![CDATA[Web系统架构与底层研发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[张宴的博客]]></copyright>
<item>
<link>http://zyan.cc/pthreads/</link>
<title><![CDATA[PHP 真正多线程的使用]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[PHP/JS/Shell]]></category>
<pubDate>Tue, 17 Dec 2013 03:17:53 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/</guid> 
<description>
<![CDATA[ 
	　　PHP 5.3 以上版本，使用pthreads PHP扩展，可以使PHP真正地支持多线程。多线程在处理重复性的循环任务，能够大大缩短程序执行时间。<br/><br/>　　我之前的文章中说过，大多数网站的性能瓶颈不在PHP服务器上，因为它可以简单地通过横向增加服务器或CPU核数来轻松应对（对于各种云主机，增加VPS或CPU核数就更方便了，直接以备份镜像增加VPS，连操作系统、环境都不用安装配置），而是在于MySQL数据库。如果用 MySQL 数据库，一条联合查询的SQL，也许就可以处理完业务逻辑，但是，遇到大量并发请求，就歇菜了。如果用 NoSQL 数据库，也许需要十次查询，才能处理完同样地业务逻辑，但每次查询都比 MySQL 要快，十次循环NoSQL查询也许比一次MySQL联合查询更快，应对几万次/秒的查询完全没问题。如果加上PHP多线程，通过十个线程同时查询NoSQL，返回结果汇总输出，速度就要更快了。我们实际的APP产品中，调用一个通过用户喜好实时推荐商品的PHP接口，PHP需要对BigSea NoSQL数据库发起500~1000次查询，来实时算出用户的个性喜好商品数据，PHP多线程的作用非常明显。<br/><br/>　　PHP扩展下载：<a href="https://github.com/krakjoe/pthreads" target="_blank">https://github.com/krakjoe/pthreads</a><br/>　　PHP手册文档：<a href="http://php.net/manual/zh/book.pthreads.php" target="_blank">http://php.net/manual/zh/book.pthreads.php</a><br/><br/>　　1、扩展的编译安装(Linux），编辑参数 --enable-maintainer-zts 是必选项：<br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">cd /Data/tgz/php-5.5.1<br/>./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<br/>make clean<br/>make<br/>make install&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/><br/>unzip pthreads-master.zip<br/>cd pthreads-master<br/>/Data/apps/php/bin/phpize<br/>./configure --with-php-config=/Data/apps/php/bin/php-config<br/>make<br/>make install<br/></div><br/><br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">vi /Data/apps/php/etc/php.ini</div><br/>添加：<br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">extension = "pthreads.so"</div><br/><br/>　　2、给出一段PHP多线程、与For循环，抓取百度搜索页面的PHP代码示例：<br/><textarea name="code" class="php" rows="15" cols="100">
<?php
&nbsp;&nbsp;class test_thread_run extends Thread 
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public $url;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public $data;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public function __construct($url)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->url = $url;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public function run()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(($url = $this->url))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->data = model_http_curl_get($url);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;function model_thread_result_get($urls_array) 
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach ($urls_array as $key => $value) 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$thread_array[$key] = new test_thread_run($value["url"]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$thread_array[$key]->start();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach ($thread_array as $thread_array_key => $thread_array_value) 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while($thread_array[$thread_array_key]->isRunning())
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usleep(10);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($thread_array[$thread_array_key]->join())
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$variable_data[$thread_array_key] = $thread_array[$thread_array_key]->data;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $variable_data;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;function model_http_curl_get($url,$userAgent="") 
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$userAgent = $userAgent ? $userAgent : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2)'; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$curl = curl_init();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($curl, CURLOPT_URL, $url);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($curl, CURLOPT_TIMEOUT, 5);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result = curl_exec($curl);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;curl_close($curl);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $result;
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;for ($i=0; $i < 100; $i++) 
&nbsp;&nbsp;&#123; 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$urls_array[] = array("name" => "baidu", "url" => "http://www.baidu.com/s?wd=".mt_rand(10000,20000));
&nbsp;&nbsp;&#125;

&nbsp;&nbsp;$t = microtime(true);
&nbsp;&nbsp;$result = model_thread_result_get($urls_array);
&nbsp;&nbsp;$e = microtime(true);
&nbsp;&nbsp;echo "多线程：".($e-$t)."&#92;n";

&nbsp;&nbsp;$t = microtime(true);
&nbsp;&nbsp;foreach ($urls_array as $key => $value) 
&nbsp;&nbsp;&#123;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result_new[$key] = model_http_curl_get($value["url"]);
&nbsp;&nbsp;&#125;
&nbsp;&nbsp;$e = microtime(true);
&nbsp;&nbsp;echo "For循环：".($e-$t)."&#92;n";
?>
</textarea><br/>Tags - <a href="http://zyan.cc/tags/php%25E5%25A4%259A%25E7%25BA%25BF%25E7%25A8%258B/" rel="tag">php多线程</a> , <a href="http://zyan.cc/tags/php/" rel="tag">php</a> , <a href="http://zyan.cc/tags/%25E5%25A4%259A%25E7%25BA%25BF%25E7%25A8%258B/" rel="tag">多线程</a> , <a href="http://zyan.cc/tags/pthreads/" rel="tag">pthreads</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40147</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>Simon &lt;pbzyong@126.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 03:26:15 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40147</guid> 
<description>
<![CDATA[ 
	学习了！！！赞
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40149</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>mcsrainbow &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 04:01:20 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40149</guid> 
<description>
<![CDATA[ 
	赞！！！
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40152</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>levsion &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 04:45:44 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40152</guid> 
<description>
<![CDATA[ 
	张师兄不错啊，现在还在研究php啊
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40154</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>啊跳 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 05:01:08 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40154</guid> 
<description>
<![CDATA[ 
	终于抢到首页了
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40160</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>maple &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 05:23:24 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40160</guid> 
<description>
<![CDATA[ 
	首页了吗？赞
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40162</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>始终不够 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 05:52:10 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40162</guid> 
<description>
<![CDATA[ 
	不稳定呢。有点鸡肋。
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40164</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>路人X &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 06:12:03 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40164</guid> 
<description>
<![CDATA[ 
	line14 if(($url = $this-&gt;url))&nbsp;&nbsp;比较值用==,恒等===
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40165</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>webyxm &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 06:26:54 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40165</guid> 
<description>
<![CDATA[ 
	赞！！！学习了！
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40166</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>risuns &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 06:30:55 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40166</guid> 
<description>
<![CDATA[ 
	一直想找这个，学习了
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40167</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>test &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 06:49:56 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40167</guid> 
<description>
<![CDATA[ 
	哇！今天发布的啊？
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40168</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>小葱 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 07:11:41 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40168</guid> 
<description>
<![CDATA[ 
	赞,学习了.
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40169</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>酱油哥 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 07:14:21 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40169</guid> 
<description>
<![CDATA[ 
	抢个
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40172</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>anono &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 07:48:38 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40172</guid> 
<description>
<![CDATA[ 
	如果只是用多线程发起网络通讯，原来张兄比较擅长的libevent是不是也可以
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40175</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>test &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 08:36:07 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40175</guid> 
<description>
<![CDATA[ 
	学习了
]]>
</description>
</item><item>
<link>http://zyan.cc/pthreads/#blogcomment40178</link>
<title><![CDATA[[评论] PHP 真正多线程的使用]]></title> 
<author>dodo &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 17 Dec 2013 09:23:07 +0000</pubDate> 
<guid>http://zyan.cc/pthreads/#blogcomment40178</guid> 
<description>
<![CDATA[ 
	回复 路人X 2013-12-17 14:12这个是赋值，不是判断等于
]]>
</description>
</item>
</channel>
</rss>