{"id":533,"date":"2015-04-27T16:29:34","date_gmt":"2015-04-27T08:29:34","guid":{"rendered":"http:\/\/www.zyuns.com\/?p=533"},"modified":"2015-04-27T17:10:07","modified_gmt":"2015-04-27T09:10:07","slug":"533","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=533","title":{"rendered":"PHP \u771f\u6b63\u591a\u7ebf\u7a0b\u7684\u4f7f\u7528"},"content":{"rendered":"<p>PHP 5.3 \u4ee5\u4e0a\u7248\u672c\uff0c\u4f7f\u7528pthreads PHP\u6269\u5c55\uff0c\u53ef\u4ee5\u4f7fPHP\u771f\u6b63\u5730\u652f\u6301\u591a\u7ebf\u7a0b\u3002\u591a\u7ebf\u7a0b\u5728\u5904\u7406\u91cd\u590d\u6027\u7684\u5faa\u73af\u4efb\u52a1\uff0c\u80fd\u591f\u5927\u5927\u7f29\u77ed\u7a0b\u5e8f\u6267\u884c\u65f6\u95f4\u3002<\/p>\n<p>\u6211\u4e4b\u524d\u7684\u6587\u7ae0\u4e2d\u8bf4\u8fc7\uff0c\u5927\u591a\u6570\u7f51\u7ad9\u7684\u6027\u80fd\u74f6\u9888\u4e0d\u5728PHP\u670d\u52a1\u5668\u4e0a\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u7b80\u5355\u5730\u901a\u8fc7\u6a2a\u5411\u589e\u52a0\u670d\u52a1\u5668\u6216CPU\u6838\u6570\u6765\u8f7b\u677e\u5e94\u5bf9\uff08\u5bf9\u4e8e\u5404\u79cd\u4e91\u4e3b\u673a\uff0c\u589e\u52a0VPS\u6216CPU\u6838\u6570\u5c31\u66f4\u65b9\u4fbf\u4e86\uff0c\u76f4\u63a5\u4ee5\u5907\u4efd\u955c\u50cf\u589e\u52a0VPS\uff0c\u8fde\u64cd\u4f5c\u7cfb\u7edf\u3001\u73af\u5883\u90fd\u4e0d\u7528\u5b89\u88c5\u914d\u7f6e\uff09\uff0c\u800c\u662f\u5728\u4e8eMySQL\u6570\u636e\u5e93\u3002\u5982\u679c\u7528 MySQL \u6570\u636e\u5e93\uff0c\u4e00\u6761\u8054\u5408\u67e5\u8be2\u7684SQL\uff0c\u4e5f\u8bb8\u5c31\u53ef\u4ee5\u5904\u7406\u5b8c\u4e1a\u52a1\u903b\u8f91\uff0c\u4f46\u662f\uff0c\u9047\u5230\u5927\u91cf\u5e76\u53d1\u8bf7\u6c42\uff0c\u5c31\u6b47\u83dc\u4e86\u3002\u5982\u679c\u7528 NoSQL \u6570\u636e\u5e93\uff0c\u4e5f\u8bb8\u9700\u8981\u5341\u6b21\u67e5\u8be2\uff0c\u624d\u80fd\u5904\u7406\u5b8c\u540c\u6837\u5730\u4e1a\u52a1\u903b\u8f91\uff0c\u4f46\u6bcf\u6b21\u67e5\u8be2\u90fd\u6bd4 MySQL \u8981\u5feb\uff0c\u5341\u6b21\u5faa\u73afNoSQL\u67e5\u8be2\u4e5f\u8bb8\u6bd4\u4e00\u6b21MySQL\u8054\u5408\u67e5\u8be2\u66f4\u5feb\uff0c\u5e94\u5bf9\u51e0\u4e07\u6b21\/\u79d2\u7684\u67e5\u8be2\u5b8c\u5168\u6ca1\u95ee\u9898\u3002\u5982\u679c\u52a0\u4e0aPHP\u591a\u7ebf\u7a0b\uff0c\u901a\u8fc7\u5341\u4e2a\u7ebf\u7a0b\u540c\u65f6\u67e5\u8be2NoSQL\uff0c\u8fd4\u56de\u7ed3\u679c\u6c47\u603b\u8f93\u51fa\uff0c\u901f\u5ea6\u5c31\u8981\u66f4\u5feb\u4e86\u3002\u6211\u4eec\u5b9e\u9645\u7684APP\u4ea7\u54c1\u4e2d\uff0c\u8c03\u7528\u4e00\u4e2a\u901a\u8fc7\u7528\u6237\u559c\u597d\u5b9e\u65f6\u63a8\u8350\u5546\u54c1\u7684PHP\u63a5\u53e3\uff0cPHP\u9700\u8981\u5bf9BigSea NoSQL\u6570\u636e\u5e93\u53d1\u8d77500~1000\u6b21\u67e5\u8be2\uff0c\u6765\u5b9e\u65f6\u7b97\u51fa\u7528\u6237\u7684\u4e2a\u6027\u559c\u597d\u5546\u54c1\u6570\u636e\uff0cPHP\u591a\u7ebf\u7a0b\u7684\u4f5c\u7528\u975e\u5e38\u660e\u663e\u3002<\/p>\n<p>PHP\u6269\u5c55\u4e0b\u8f7d\uff1a<a href=\"https:\/\/github.com\/krakjoe\/pthreads\" target=\"_blank\">https:\/\/github.com\/krakjoe\/pthreads<\/a><br \/>\nPHP\u624b\u518c\u6587\u6863\uff1a<a href=\"http:\/\/php.net\/manual\/zh\/book.pthreads.php\" target=\"_blank\">http:\/\/php.net\/manual\/zh\/book.pthreads.php<\/a><br \/>\nwindows\u6269\u5c55\u4e0b\u8f7d:\u00a0<a href=\"http:\/\/windows.php.net\/downloads\/pecl\/releases\/pthreads\/\" target=\"_blank\">http:\/\/windows.php.net\/downloads\/pecl\/releases\/pthreads\/<\/a><\/p>\n<p>1\u3001\u6269\u5c55\u7684\u7f16\u8bd1\u5b89\u88c5(Linux\uff09\uff0c\u7f16\u8f91\u53c2\u6570 &#8211;enable-maintainer-zts \u662f\u5fc5\u9009\u9879\uff1a<\/p>\n<pre class=\"lang:default decode:true \">cd \/Data\/tgz\/php-5.5.1\r\n.\/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\r\nmake clean\r\nmake\r\nmake install        \r\n\r\nunzip pthreads-master.zip\r\ncd pthreads-master\r\n\/Data\/apps\/php\/bin\/phpize\r\n.\/configure --with-php-config=\/Data\/apps\/php\/bin\/php-config\r\nmake\r\nmake install<\/pre>\n<pre class=\"lang:default decode:true \">vi \/Data\/apps\/php\/etc\/php.ini<\/pre>\n<p>\u6dfb\u52a0\uff1a<\/p>\n<pre class=\"lang:default decode:true \">extension = \"pthreads.so\"<\/pre>\n<p>2\u3001\u7ed9\u51fa\u4e00\u6bb5PHP\u591a\u7ebf\u7a0b\u3001\u4e0eFor\u5faa\u73af\uff0c\u6293\u53d6\u767e\u5ea6\u641c\u7d22\u9875\u9762\u7684PHP\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre class=\"lang:default decode:true \">&lt;?php  \r\n  class test_thread_run extends Thread   \r\n  {  \r\n      public $url;  \r\n      public $data;  \r\n  \r\n      public function __construct($url)  \r\n      {  \r\n          $this-&gt;url = $url;  \r\n      }  \r\n  \r\n      public function run()  \r\n      {  \r\n          if(($url = $this-&gt;url))  \r\n          {  \r\n              $this-&gt;data = model_http_curl_get($url);  \r\n          }  \r\n      }  \r\n  }  \r\n  \r\n  function model_thread_result_get($urls_array)   \r\n  {  \r\n      foreach ($urls_array as $key =&gt; $value)   \r\n      {  \r\n          $thread_array[$key] = new test_thread_run($value[\"url\"]);  \r\n          $thread_array[$key]-&gt;start();  \r\n      }  \r\n  \r\n      foreach ($thread_array as $thread_array_key =&gt; $thread_array_value)   \r\n      {  \r\n          while($thread_array[$thread_array_key]-&gt;isRunning())  \r\n          {  \r\n              usleep(10);  \r\n          }  \r\n          if($thread_array[$thread_array_key]-&gt;join())  \r\n          {  \r\n              $variable_data[$thread_array_key] = $thread_array[$thread_array_key]-&gt;data;  \r\n          }  \r\n      }  \r\n      return $variable_data;  \r\n  }  \r\n  \r\n  function model_http_curl_get($url,$userAgent=\"\")   \r\n  {  \r\n      $userAgent = $userAgent ? $userAgent : 'Mozilla\/4.0 (compatible; MSIE 7.0; Windows NT 5.2)';   \r\n      $curl = curl_init();  \r\n      curl_setopt($curl, CURLOPT_URL, $url);  \r\n      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  \r\n      curl_setopt($curl, CURLOPT_TIMEOUT, 5);  \r\n      curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);  \r\n      $result = curl_exec($curl);  \r\n      curl_close($curl);  \r\n      return $result;  \r\n  }  \r\n  \r\n  for ($i=0; $i &lt; 100; $i++)   \r\n  {   \r\n      $urls_array[] = array(\"name\" =&gt; \"baidu\", \"url\" =&gt; \"http:\/\/www.baidu.com\/s?wd=\".mt_rand(10000,20000));  \r\n  }  \r\n  \r\n  $t = microtime(true);  \r\n  $result = model_thread_result_get($urls_array);  \r\n  $e = microtime(true);  \r\n  echo \"\u591a\u7ebf\u7a0b\uff1a\".($e-$t).\"\\n\";  \r\n  \r\n  $t = microtime(true);  \r\n  foreach ($urls_array as $key =&gt; $value)   \r\n  {  \r\n      $result_new[$key] = model_http_curl_get($value[\"url\"]);  \r\n  }  \r\n  $e = microtime(true);  \r\n  echo \"For\u5faa\u73af\uff1a\".($e-$t).\"\\n\";  \r\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP 5.3 \u4ee5\u4e0a\u7248\u672c\uff0c\u4f7f\u7528pthreads PHP\u6269\u5c55\uff0c\u53ef\u4ee5\u4f7fPHP\u771f\u6b63\u5730\u652f\u6301\u591a\u7ebf\u7a0b\u3002\u591a\u7ebf\u7a0b\u5728\u5904\u7406\u91cd\u590d\u6027\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[4,14],"class_list":["post-533","post","type-post","status-publish","format-standard","hentry","category-php","tag-php","tag-14"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/533","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=533"}],"version-history":[{"count":4,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions"}],"predecessor-version":[{"id":537,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/533\/revisions\/537"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}