Home  >  Article  >  Backend Development  >  pthreads multi-threaded data collection

pthreads multi-threaded data collection

WBOY
WBOYOriginal
2016-07-29 09:07:30939browse

In the past, multi-threading using curl was not real multi-threading, it was just a simulated multi-threading. Now pthreads is used to achieve true multi-threading.

Download:

 Windows:

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

  Under mac, unix, linux:

  https://github. com/krakjoe/pthreads

Installation method:

 Windows:

  Unzip the files to get pthreadVC2.dll and php_pthreads.dll, put the vc2 file into the same level directory as php.exe, and put php_pthreads.dll into the extension directory.

   Modify the php.ini file and add extension=php_pthreads.dll

                                                                                             int in in in in php. Blog http://blog.s135.com/pthreads/

Calling method:

For specific usage, you can also refer to Brother Yan’s blog http://blog.s135.com/pthreads/

It can also be combined with the previous get_html This is how to implement the class

 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 }
pthreads multi-threaded data collection

pthreads multi-threaded data collection The above introduces pthreads multi-threaded data collection, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:play with phpNext article:play with php