zoukankan      html  css  js  c++  java
  • pthreads多线程数据采集

    以前使用curl的多线程并不是真正的多线程,只是一种模拟的多线程,现在使用pthreads来实现真正意义上的多线程。

    下载:

      windows下:

        http://windows.php.net/downloads/pecl/releases/pthreads/0.0.45/

      mac、unix、linux下:

        https://github.com/krakjoe/pthreads

    安装方式:

      windows下:

        解压得到pthreadVC2.dll和php_pthreads.dll文件,把vc2文件放到php.exe同级目录,把php_pthreads.dll放到扩展目录下。

        修改php.ini文件 添加extension=php_pthreads.dll

        修改Apache配置文件httpd.conf 添加LoadFile "yourpath/php/pthreadVC2.dll"

      mac、unix、linux下:

        具体可参考宴哥的博客http://blog.s135.com/pthreads/

    调用方式:

      具体的用法也可以参考宴哥的博客http://blog.s135.com/pthreads/

      结合以前的get_html也可以这样来实现类

     1 class threads extends Thread
     2 {
     3     public $url = '';
     4     public $options = array();
     5     public $data;
     6 
     7     public function __construct($url, $options = array()){
     8         $this->url = $url;
     9         $this->options = $options;
    10     }
    11 
    12     public function run(){
    13         if(!empty($this->url)){
    14             $this->data = $this->get_html($this->url, $this->options);
    15         }
    16     }
    17 
    18     public function get_html($url,$options = array()){
    19         if(empty($options)){
    20             $options[CURLOPT_RETURNTRANSFER] = true;
    21             $options[CURLOPT_TIMEOUT] = 5;
    22         }
    23         $ch = curl_init($url);
    24         curl_setopt_array($ch,$options);
    25         $html = curl_exec($ch);
    26         curl_close($ch);
    27         if($html === false){
    28             return false;
    29         }
    30         return $html;
    31     }
    32 }

     

  • 相关阅读:
    python 面向对象编程的三大特征之一 多态
    python 访问控制
    python 面向对象编程的三大特征之一 继承
    朱兆祺教你如何攻破C语言学习、笔试与机试的难点
    如何画好流程图
    2013年个人计划
    30天敏捷结果(1):总体认识Getting Result敏捷方法
    每天一个linux命令目录
    国嵌C语言学习
    【head first python】1.初识python 人人都爱列表
  • 原文地址:https://www.cnblogs.com/yanghaoinbeijing/p/3507175.html
Copyright © 2011-2022 走看看