pthreads多線程數據採集

之前使用curl的多線程並非真正的多線程,只是一種模擬的多線程,如今使用pthreads來實現真正意義上的多線程。php

下載:html

  windows下:linux

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

  mac、unix、linux下:github

    https://github.com/krakjoe/pthreadswindows

安裝方式:多線程

  windows下:curl

    解壓獲得pthreadVC2.dll和php_pthreads.dll文件,把vc2文件放到php.exe同級目錄,把php_pthreads.dll放到擴展目錄下。this

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

    修改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 }
相關文章
相關標籤/搜索