最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用pthreads實(shí)現(xiàn)真正的PHP多線程(需PHP5.3以上版本)

 更新時(shí)間:2014年05月05日 09:09:46   作者:  
PHP 5.3 以上版本,使用pthreads PHP擴(kuò)展,可以使PHP真正地支持多線程。多線程在處理重復(fù)性的循環(huán)任務(wù),能夠大大縮短程序執(zhí)行時(shí)間

我之前的文章中說過,大多數(shù)網(wǎng)站的性能瓶頸不在PHP服務(wù)器上,因?yàn)樗梢院唵蔚赝ㄟ^橫向增加服務(wù)器或CPU核數(shù)來輕松應(yīng)對(對于各種云主機(jī),增加VPS或CPU核數(shù)就更方便了,直接以備份鏡像增加VPS,連操作系統(tǒng)、環(huán)境都不用安裝配置),而是在于MySQL數(shù)據(jù)庫。

如果用 MySQL 數(shù)據(jù)庫,一條聯(lián)合查詢的SQL,也許就可以處理完業(yè)務(wù)邏輯,但是,遇到大量并發(fā)請求,就歇菜了。

如果用 NoSQL 數(shù)據(jù)庫,也許需要十次查詢,才能處理完同樣地業(yè)務(wù)邏輯,但每次查詢都比 MySQL 要快,十次循環(huán)NoSQL查詢也許比一次MySQL聯(lián)合查詢更快,應(yīng)對幾萬次/秒的查詢完全沒問題。

如果加上PHP多線程,通過十個線程同時(shí)查詢NoSQL,返回結(jié)果匯總輸出,速度就要更快了。我們實(shí)際的APP產(chǎn)品中,調(diào)用一個通過用戶喜好實(shí)時(shí)推薦商品的PHP接口,PHP需要對BigSea NoSQL數(shù)據(jù)庫發(fā)起500~1000次查詢,來實(shí)時(shí)算出用戶的個性喜好商品數(shù)據(jù),PHP多線程的作用非常明顯。

PHP擴(kuò)展下載:https://github.com/krakjoe/pthreads
PHP手冊文檔:http://php.net/manual/zh/book.pthreads.php

1、擴(kuò)展的編譯安裝(Linux),編譯參數(shù) --enable-maintainer-zts 是必選項(xiàng):

復(fù)制代碼 代碼如下:

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

php.ini中添加:

復(fù)制代碼 代碼如下:

vi /Data/apps/php/etc/php.ini
extension = "pthreads.so"

給出一段PHP多線程、與For循環(huán),抓取百度搜索頁面的PHP代碼示例:

復(fù)制代碼 代碼如下:

<?php
  class test_thread_run extends Thread
  {
      public $url;
      public $data;

      public function __construct($url)
      {
          $this->url = $url;
      }

      public function run()
      {
          if(($url = $this->url))
          {
              $this->data = model_http_curl_get($url);
          }
      }
  }

  function model_thread_result_get($urls_array)
  {
      foreach ($urls_array as $key => $value)
      {
          $thread_array[$key] = new test_thread_run($value["url"]);
          $thread_array[$key]->start();
      }

      foreach ($thread_array as $thread_array_key => $thread_array_value)
      {
          while($thread_array[$thread_array_key]->isRunning())
          {
              usleep(10);
          }
          if($thread_array[$thread_array_key]->join())
          {
              $variable_data[$thread_array_key] = $thread_array[$thread_array_key]->data;
          }
      }
      return $variable_data;
  }

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

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

  $t = microtime(true);
  $result = model_thread_result_get($urls_array);
  $e = microtime(true);
  echo "多線程:".($e-$t)."
";

  $t = microtime(true);
  foreach ($urls_array as $key => $value)
  {
      $result_new[$key] = model_http_curl_get($value["url"]);
  }
  $e = microtime(true);
  echo "For循環(huán):".($e-$t)."
";
?>

相關(guān)文章

  • PHP調(diào)用VC編寫的COM組件實(shí)例

    PHP調(diào)用VC編寫的COM組件實(shí)例

    最近項(xiàng)目需要PHP來解析二進(jìn)制數(shù)據(jù),如果直接PHP做比較麻煩,就想到VC編寫COM組件來做,提供PHP調(diào)用。文章將介紹VC編寫COM組件提供PHP調(diào)用,實(shí)現(xiàn)一個計(jì)算兩個數(shù)字相加的功能
    2014-03-03
  • PHP編寫daemon process 實(shí)例詳解

    PHP編寫daemon process 實(shí)例詳解

    這篇文章主要介紹了PHP編寫daemon process 實(shí)例詳解的相關(guān)資料,這里提供實(shí)例代碼,及詳細(xì)講解,需要的朋友可以參考下
    2016-11-11
  • 淺談PHP中關(guān)于foreach使用引用變量的坑

    淺談PHP中關(guān)于foreach使用引用變量的坑

    下面小編就為大家?guī)硪黄狿HP不使用遞歸的無限級分類的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11
  • php 實(shí)現(xiàn)一個字符串加密解密的函數(shù)實(shí)例代碼

    php 實(shí)現(xiàn)一個字符串加密解密的函數(shù)實(shí)例代碼

    php開發(fā)中,我們經(jīng)常會對字符串進(jìn)行加密解密操作,本文章向大家分享一個php字符串加密解密的函數(shù),需要的朋友可以參考一下
    2016-11-11
  • 淺談PHP發(fā)送HTTP請求的幾種方式

    淺談PHP發(fā)送HTTP請求的幾種方式

    這篇文章主要介紹了淺談PHP發(fā)送HTTP請求的幾種方式,整理一下除了使用 cURL 外 PHP 發(fā)送 HTTP 請求的方式,有興趣的可以了解一下。
    2017-07-07
  • 淺談PHP解析URL函數(shù)parse_url和parse_str

    淺談PHP解析URL函數(shù)parse_url和parse_str

    這篇文章主要介紹了PHP解析URL函數(shù)parse_url和parse_str,并給出了相應(yīng)的示例,非常的實(shí)用,有需要的朋友們可以參考下
    2014-11-11
  • Laravel事件監(jiān)聽器用法實(shí)例分析

    Laravel事件監(jiān)聽器用法實(shí)例分析

    這篇文章主要介紹了Laravel事件監(jiān)聽器用法,結(jié)合實(shí)例形式分析了Laravel事件監(jiān)聽器的注冊、使用相關(guān)流程及操作技巧,需要的朋友可以參考下
    2019-03-03
  • PHP解析html類庫simple_html_dom的轉(zhuǎn)碼bug

    PHP解析html類庫simple_html_dom的轉(zhuǎn)碼bug

    這篇文章主要介紹了PHP解析html類庫simple_html_dom的轉(zhuǎn)碼bug ,需要的朋友可以參考下
    2014-05-05
  • PHP等比例壓縮圖片的實(shí)例代碼

    PHP等比例壓縮圖片的實(shí)例代碼

    本文通過一段簡單的代碼給大家介紹PHP等比例壓縮圖片的方法,代碼簡單易懂,非常不錯,具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-07-07
  • php for 循環(huán)使用的簡單實(shí)例

    php for 循環(huán)使用的簡單實(shí)例

    下面小編就為大家?guī)硪黄猵hp for 循環(huán)使用的簡單實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06

最新評論

耿马| 阿巴嘎旗| 潞城市| 奉节县| 湘潭县| 岑巩县| 高唐县| 阳东县| 南木林县| 吉林省| 仁寿县| 泰宁县| 苏尼特左旗| 兴安盟| 灵武市| 毕节市| 肃北| 寻乌县| 晋中市| 海丰县| 大余县| 宁蒗| 乌苏市| 延津县| 宁明县| 桦南县| 临江市| 茂名市| 通渭县| 新巴尔虎右旗| 施甸县| 隆林| 綦江县| 徐汇区| 莱西市| 莲花县| 太仆寺旗| 九台市| 秦皇岛市| 博野县| 博兴县|