您的位置:68399皇家赌场 > 集群主机 > 皇家赌场:PHP 真正二十四线程的施用

皇家赌场:PHP 真正二十四线程的施用

发布时间:2019-07-21 00:15编辑:集群主机浏览(52)

    复制代码 代码如下:

    }

     

    foreach ($urls as $i => $url) {
    $data = curl_multi_getcontent($conn[$i]); // 得到爬取的代码字符串
    fwrite($st,$data); // 将字符串写入文件。当然,也足以不写入文件,比如存入数据库
    } // 得到数据变量,并写入文件

    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

     2、给出一段PHP二十四线程、与For循环,抓取百度找出页面包车型客车PHP代码示例:

    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

     

    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       

    <?php

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

    丰硕扩大:

      for ($i=0; $i < 100; $i )
      {
          $urls_array[] = array("name" => "baidu", "url" => "));
      }

    $this->url = $url;

      PHP扩充下载:
      PHP手册文书档案:

    PHP扩张下载:https://github.com/krakjoe/pthreads
    PHP手册文书档案:

    您恐怕感兴趣的稿子:

    • Ubuntu12下编写翻译安装PHP5.3开辟遇到
    • php5.3不可能三番五次mssql数据库的消除办法
    • php5.3今后的本子连接sqlserver三千的不二秘籍
    • PHP5.3与5.5放弃与过期函数整理汇总
    • php5.3 goto函数介绍和示范
    • php5.3 不支持 session_register() 此函数已启用的解决方式
    • php5.3 注意事项表明
    • php5.3 屏弃函数小结
    • PHP5.3.1 不再帮忙ISAPI
    • PHP5.3新特性小结

    $this->data = model_http_curl_get($url);

          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;
      }

    do {
    curl_multi_exec($mh,$active);
    } while ($active);

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

    $e = microtime(true);

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

    PHP 5.3 以上版本,使用pthreads PHP扩张,可以使PHP真正地支撑多线程。二十八线程在管理重复性的巡回职分,能够大大裁减程序推行时间。

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

    {

      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 ($urls as $i => $url) {
    curl_multi_remove_handle($mh,$conn[$i]);
    curl_close($conn[$i]);
    }

      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();
          }

    = model_http_curl_get($url);

      小编事先的篇章中说过,大好多网址的性情瓶颈不在PHP服务器上,因为它能够大致地通过横向扩展服务器或CPU核数来轻巧应对(对于种种云主机,扩充VPS或CPU核数就更有利了,直接以备份镜像增添VPS,连操作系统、意况都不用安装配置),而是在于MySQL数据库。假设用 MySQL 数据库,一条联合查询的SQL,恐怕就足以拍卖完专门的学问逻辑,可是,遇到多量产出央求,就歇菜了。倘诺用 NoSQL 数据库,恐怕须求10次查询,本事管理完同样地职业逻辑,但老是查询都比 MySQL 要快,12遍循环NoSQL查询大概比三回MySQL联合查询更加快,应对几万次/秒的询问完全没问题。若是加上PHP多线程,通过11个线程相同的时间询问NoSQL,重返结果汇总输出,速度将要更加快了。大家其实的应用程式产品中,调用贰个经过用户喜好实时推荐商品的PHP接口,PHP供给对BigSea NoSQL数据库发起500~一千次查询,来实时算出用户的秉性喜好商品数量,PHP多线程的效应丰硕醒目。

    1、扩张的编写翻译安装(Linux),编辑参数 --enable-maintainer-zts 是必选项:

    PHP扩张下载:
    PHP手册文书档案:

    $variable_data[$thread_array_key] = $thread_array[$thread_array_key]->data;

    popen

    一段PHP八线程、与For循环,抓取百度寻找页面包车型客车PHP代码示例:

    复制代码 代码如下:

    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        

     皇家赌场 1

    $save_to='/test.txt'; // 把抓取的代码写入该公文
    $st = fopen($save_to,"a");

    <?php
      class test_thread_run extends Thread
      {
          public $url;
          public $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;
      }

    事例,收集数据

    1、扩张的编写翻译安装(Linux),编写翻译参数 --enable-maintainer-zts 是必选项:

    {

     

    <?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" => ")); 
      } 
     
      $t = microtime(true); 
      $result = model_thread_result_get($urls_array); 
      $e = microtime(true); 
      echo "多线程:".($e-$t)."n"; 
     
      $t = microtime(true); 
      foreach ($urls_array as $key => $value)  
      { 
          $result_new[$key] = model_http_curl_get($value["url"]); 
      } 
      $e = microtime(true); 
      echo "For循环:".($e-$t)."n"; 
    ?>

      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;
      }

    if($pid==-1){ //进度成立失利

     

    <?php
    $urls = array(
    '',
    '',
    ''
    );

          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;
      }

    }

      for ($i=0; $i < 100; $i )
      {
          $urls_array[] = array("name" => "baidu", "url" => "));
      }

    curl_multi_close($mh);
    fclose($st);
    ?>

    复制代码 代码如下:

    $curl = curl_init();

     

     extension = "pthreads.so"

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

    }

      1、扩张的编写翻译安装(Linux),编辑参数 --enable-maintainer-zts 是必选项:

    cd /Data/tgz/php-5.3.8
    ./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

    万一用 NoSQL 数据库,或许须要12遍查询,本事管理完一样地专门的学业逻辑,但老是查询都比 MySQL 要快,10次循环NoSQL查询只怕比叁遍MySQL联合查询越来越快,应对几万次/秒的询问完全没难点。

    }

    PHP 5.3 以上版本,使用pthreads PHP扩充,能够使PHP真正地支撑四线程。多线程在管理重复性的巡回职责,能够大大缩小程序施行时间。

     vi /Data/apps/php/etc/php.ini

    假若加上PHP多线程,通过12个线程同不常候询问NoSQL,再次来到结果汇总输出,速度将在更加快了。大家实际上的APP产品中,调用三个因而用户喜好实时推荐商品的PHP接口,PHP须求对BigSea NoSQL数据库发起500~1000次询问,来实时算出用户的特性喜好物品数量,PHP四线程的作用十二分显然。

    curl_close($curl);

    添加:

    $mh = curl_multi_init();
    foreach ($urls as $i => $url) {
    $conn[$i] = curl_init($url);
    curl_setopt($conn[$i], CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
    curl_setopt($conn[$i], CURLOPT_HEADER ,0);
    curl_setopt($conn[$i], CURLOPT_CONNECTTIMEOUT,60);
    curl_setopt($conn[$i],CURLOPT_RETU奥迪Q7NTRANSFE奇骏,true); // 设置不将爬代替码写到浏览器,而是转化为字符串
    curl_multi_add_handle ($mh,$conn[$i]);
    }

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

    array('/apps/bin/launcher.php','charge/promotion_props_stat.php','click',1),

    extension = "pthreads.so"

     代码如下

    php.ini中添加:

    public function __construct($url)

    皇家赌场 2

     代码如下

    交由一段PHP四线程、与For循环,抓取百度查寻页面包车型地铁PHP代码示例:

    echo "For循环:".($e-$t)."n";

    vi /Data/apps/php/etc/php.ini

    小编事先的篇章中说过,大比比较多网址的质量瓶颈不在PHP服务器上,因为它能够简单地通过横向扩充服务器或CPU核数来轻巧应对(对于各类云主机,扩大VPS或CPU核数就更有益了,直接以备份镜像扩展VPS,连操作系统、境遇都不用安装配置),而是在于MySQL数据库。

    public function run()

     

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

    $thread_array[$key]

    皇家赌场 3

    一旦用 MySQL 数据库,一条联合查询的SQL,恐怕就能够拍卖完事情逻辑,然则,遭受大批量面世恳求,就歇菜了。

    添加:

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

    $thread_array[$key]->start();

    首先popen()函数创设一条管道,方向为父进度的fpout到子进程的stdin,接着popen程序张开in.txt文本并一行一行地读抽出来写到管道的fpout端。子进度则从stdin中读取从父进程发送过来的数量呈现到stdout中。

    class test_thread_run extends Thread

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

    CURLOPT_USERAGENT, $userAgent);

    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        

    {

      2、给出一段PHP四线程、与For循环,抓取百度搜索页面包车型大巴PHP代码示例:

    function model_thread_result_get($urls_array)

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

    for ($i=0; $i < 100; $i )

    }

    }

    {

    if(($url = $this->url))

    die('fork child process failure!');

    }

     

    $e = microtime(true);

     

    if($thread_array[$thread_array_key]->join())

    PHP 5.3 以上版本,使用pthreads PHP扩展,能够使PHP真正地帮助多线程。四线程在拍卖重复性的巡回任务,能够大大缩小程序施行时间。

    $t = microtime(true);

     

    else{ //子进度管理逻辑

    = $url;

    curl_setopt($curl, CURLOPT_URL,

      PHP增加下载:
      PHP手册文书档案:

    由于php的进度是不接济二十四线程的,有些场景为了便于以及坚实品质,能够用php完毕多进度以弥补那一个不足:

    extension = "pthreads.so"

    return $result;

    pcntl_wait($status,WNOHANG);

    return $variable_data;

    ?>

    本文由68399皇家赌场发布于集群主机,转载请注明出处:皇家赌场:PHP 真正二十四线程的施用

    关键词: 68399皇家赌场 PHP

上一篇:PHP获取MAC地址的函数代码

下一篇:没有了