Analysis of multi-thread internal multithreading instances in PHP
This article mainly introduces the PHP multi-thread internal multi-threading, example analysis of PHP multi-threading skills, with a certain reference value, the need for friends can refer to the next
In this paper, we analyze the multi-thread usage within PHP. Share to everyone for your reference. Specific as follows:
The code is as follows:
Class Http_multirequest
{
List of URLs to crawl in parallel
Private $urls = Array ();
Options for Curl
Private $options;
constructor function
function __construct ($options = Array ())
{
$this->setoptions ($options);
}
Set up a URL list
function Seturls ($urls)
{
$this->urls = $urls;
return $this;
}
Setting options
function SetOptions ($options)
{
$options [Curlopt_returntransfer] = 1;
if (Isset ($options [' http_post ']))
{
curl_setopt ($ch, Curlopt_post, 1);
curl_setopt ($ch, Curlopt_postfields, $options [' http_post ']);
unset ($options [' http_post ']);
}
if (!isset ($options [curlopt_useragent]))
{
$options [Curlopt_useragent] = ' mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;) ';
}
if (!isset ($options [curlopt_followlocation]))
{
$options [Curlopt_followlocation] = 1;
}
if (!isset ($options [Curlopt_header]))
{
$options [Curlopt_header] = 0;
}
$this->options = $options;
}
Crawl all content in parallel
function exec ()
{
if (Empty ($this->urls) | |!is_array ($this->urls))
{
return false;
}
$curl = $data = Array ();
$MH = Curl_multi_init ();
foreach ($this->urls as $k = $v)
{
$curl [$k] = $this->addhandle ($MH, $v);
}
$this->execmulithandle ($MH);
foreach ($this->urls as $k = $v)
{
$data [$k] = Curl_multi_getcontent ($curl [$k]);
Curl_multi_remove_handle ($MH, $curl [$k]);
}
Curl_multi_close ($MH);
return $data;
}
Crawl only one page of content.
function Execone ($url)
{
if (empty ($url)) {
return false;
}
$ch = Curl_init ($url);
$this->setoneoption ($ch);
$content = curl_exec ($ch);
Curl_close ($ch);
return $content;
}
An intrinsic function that sets an option for a handle
Private Function Setoneoption ($ch)
{
Curl_setopt_array ($ch, $this->options);
}
Add a new parallel fetch handle
Private Function AddHandle ($MH, $url)
{
$ch = Curl_init ($url);
$this->setoneoption ($ch);
Curl_multi_add_handle ($MH, $ch);
return $ch;
}
Parallel execution (This is a common mistake, and I'm still using this notation here
Downloading a small file can cause the cup to occupy 100%, and the loop will run more than 100,000 times
This is a typical error that does not understand the principle. This error is quite common in the official PHP documentation. )
Private Function Execmulithandle ($MH)
{
$running = null;
do {
Curl_multi_exec ($MH, $running);
} while ($running > 0);
}
}
/* Below is an example of a test for the class above: */
$urls = Array ("http://baidu.com", "http://baidu.com", "http://baidu.com", "http://baidu.com", "http://baidu.com", " Http://baidu.com "," http://www.google.com "," http://www.sina.com.cn ",);
$m = new Http_multirequest ();
$t = Microtime (true);
$m->seturls ($urls);
Parallel fetch (parallel fetch):
$data = $m->exec ();
$parallel _time = Microtime (True)-$t;
Echo $parallel _time. "\ n";
$t = Microtime (true);
Serial fetch (serial fetch):
foreach ($urls as $url)
{
$data [] = $m->execone ($url);
}
$serial _time = Microtime (True)-$t;
Echo $serial _time. "\ n";
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/965524.html www.bkjia.com true http://www.bkjia.com/PHPjc/965524.html techarticle The analysis of multi-thread internal multithreading example in multiple thread of PHP this article mainly introduces the multi-threading of PHP Multi-threads, the example analysis of the use of PHP Multi-threading skills, with a certain reference value, need ...