Home >Backend Development >PHP Tutorial >PHP multi-threading internal multi-threading instance analysis, PHP multi-threading instance analysis_PHP tutorial
This article analyzes the internal multi-threading usage of PHP multi-threading. Share it with everyone for your reference. The details are as follows:
Copy code The code is as follows:
class Http_MultiRequest
{
//List of urls to be crawled in parallel
Private $urls = array();
//curl options
private $options;
//Constructor
Function __construct($options = array())
{
$this->setOptions($options);
}
//Set url list
Function setUrls($urls)
{
$this->urls = $urls;
return $this;
}
//Set 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;
}
//Fetch 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;
}
//Only grab the content of one web page.
Function execOne($url)
{
if (empty($url)) {
return false;
}
$ch = curl_init($url);
$this->setOneOption($ch);
$content = curl_exec($ch);
curl_close($ch);
return $content;
}
//Internal function, set the options of a certain handle
Private function setOneOption($ch)
{
curl_setopt_array($ch, $this->options);
}
//Add a new parallel crawl handle
Private function addHandle($mh, $url)
{
$ch = curl_init($url);
$this->setOneOption($ch);
curl_multi_add_handle($mh, $ch);
return $ch;
}
//Parallel execution (this way of writing is a common mistake, I still use this way of writing here, this way of writing
//Downloading a small file may cause the cup to occupy 100%, and this cycle will run more than 100,000 times
//This is a typical error caused by not understanding the principle. This error is quite common in PHP official documentation. )
Private function execMulitHandle($mh)
{
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running > 0);
}
}
/*The following is an example of a test for the above class:*/
$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 will be helpful to everyone’s PHP programming design.