一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - 使用pthreads實現(xiàn)真正的PHP多線程(需PHP5.3以上版本)

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

2020-06-27 15:25PHP教程網(wǎng) PHP教程

PHP 5.3 以上版本,使用pthreads PHP擴展,可以使PHP真正地支持多線程。多線程在處理重復(fù)性的循環(huán)任務(wù),能夠大大縮短程序執(zhí)行時間

我之前的文章中說過,大多數(shù)網(wǎng)站的性能瓶頸不在PHP服務(wù)器上,因為它可以簡單地通過橫向增加服務(wù)器或CPU核數(shù)來輕松應(yīng)對(對于各種云主機,增加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多線程,通過十個線程同時查詢NoSQL,返回結(jié)果匯總輸出,速度就要更快了。我們實際的APP產(chǎn)品中,調(diào)用一個通過用戶喜好實時推薦商品的PHP接口,PHP需要對BigSea NoSQL數(shù)據(jù)庫發(fā)起500~1000次查詢,來實時算出用戶的個性喜好商品數(shù)據(jù),PHP多線程的作用非常明顯。

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

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

復(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)."
";
?>

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 4455永久在线视频观看 | 91在线 在线播放 | 美女被的视频 | 欧美一区二区三区四区五区六区 | 欧美a级v片不卡在线观看 | 国内精品中文字幕 | 男人午夜免费视频 | 亚洲va国产日韩欧美精品色婷婷 | 99久久国产综合精品1尤物 | 黑人女性猛交xxxxxⅹxx | 四虎永久成人免费 | 国产精品久久久久a影院 | 色婷婷六月天 | 美女奶口隐私免费视频网站 | 国产a免费观看 | 五月天91 | 亚洲日本在线观看网址 | 免费看打屁股视频的软件 | 五月婷婷丁香在线视频 | 美女福利视频一区二区 | 91嫩草国产在线观看免费 | 青青草在线播放 | 日本午夜小视频 | 国产欧美一区二区成人影院 | 亚洲天堂中文 | 亚洲欧美天堂综合久久 | 99国内精品久久久久久久黑人 | 国产精品女同久久免费观看 | 波多野结衣之高校教师 | 喜马拉雅听书免费版 | 91久久偷偷做嫩草影院免费 | 国产亚洲精品第一综合linode | 国产欧美日韩高清专区ho | 国产欧美精品一区二区三区–老狼 | 嫩草影院永久一二三入口 | 精品人伦一区二区三区潘金莲 | 性美国人xxxxx18 | 日本免费高清在线观看播放 | 日本无卡无吗中文免费 | 特黄特级高清免费视频毛片 | ai换脸明星造梦工厂忘忧草 |