PHP multi-thread Internal multithreaded instance analysis, PHP multithreaded instance Analysis _php Tutorial

Source: Internet
Author: User

PHP Multi-thread internal multi-threaded instance analysis, PHP multithreaded instance analysis


In this paper, we analyze the multi-thread usage within PHP. Share to everyone for your reference. Specific as follows:

Copy CodeThe code is as follows: <?php
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/965358.html www.bkjia.com true http://www.bkjia.com/PHPjc/965358.html techarticle PHP Multi-thread internal multi-threaded instance analysis, PHP multi-threaded instance analysis in this paper, we analyze the multi-thread usage of PHP. Share to everyone for your reference. Specific as follows: Complex ...

  • 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.