PHP multi-Thread Internal multithreading example analysis _php skills

Source: Internet
Author: User
Tags curl

This article analyzes the internal multithreading usage of the PHP multi-line thread. Share to everyone for your reference. Specifically as follows:

Copy Code code as follows:
<?php
Class Http_multirequest
{
List of URLs to crawl in parallel
Private $urls = Array ();
Options for Curl
Private $options;
Constructors
function __construct ($options = Array ())
{
$this->setoptions ($options);
}
Set 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 the content of only one 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 functions, setting an option for a 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 (Such a writing is a common mistake, I still use this method here, this writing
Downloading a small file can cause the cup to occupy 100%, and this cycle will run more than 100,000 times
This is a typical error that does not understand the principle. This error is fairly common in PHP's official 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 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 crawl):
$data = $m->exec ();
$parallel _time = Microtime (True)-$t;
Echo $parallel _time. "\ n";
$t = Microtime (true);
Serial fetch (serial crawl):
foreach ($urls as $url)
{
$data [] = $m->execone ($url);
}
$serial _time = Microtime (True)-$t;
Echo $serial _time. "\ n";

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.