PHP multi-thread internal multi-thread instance analysis, php multi-thread instance Analysis _ PHP Tutorial

Source: Internet
Author: User
Tags curl options php multithreading
PHP multi-thread internal multi-thread instance analysis, php multi-thread instance analysis. PHP multi-threaded internal multi-threaded instance analysis, php multi-threaded instance Analysis This article analyzes the use of PHP multi-threaded internal multi-threaded. Share it with you for your reference. Details are as follows: Analysis of multi-threaded internal PHP instances and analysis of php multi-threaded instances

This article analyzes the internal multithreading usage of PHP multithreading. Share it with you for your reference. The details are as follows:

The code is as follows: <? Php
Class Http_MultiRequest
{
// List of URLs to be captured in parallel
Private $ urls = array ();
// Curl options
Private $ options;
// Constructor
Function _ construct ($ options = array ())
{
$ This-> setOptions ($ options );
}
// Set the 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;
}
// Capture all contents 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 capture the content of one webpage.
Function execOne ($ url)
{
If (empty ($ url )){
Return false;
}
$ Ch = curl_init ($ url );
$ This-> setOneOption ($ ch );
$ Content = curl_exec ($ ch );
Curl_close ($ ch );
Return $ content;
}
// Set the handle option for an internal function
Private function setOneOption ($ ch)
{
Curl_setopt_array ($ ch, $ this-> options );
}
// Add a new parallel 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. I still use this method here.
// Download a small file may cause 100% of the CPU usage, and this loop will run more than 0.1 million times
// This is a typical error that does not understand the principle. This error is quite common in official PHP documents .)
Private function execMulitHandle ($ mh)
{
$ Running = null;
Do {
Curl_multi_exec ($ mh, $ running );
} While ($ running> 0 );
}
}
/* The following is an example of the test of the above class :*/
$ Urls = array ("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 capture ):
$ Data = $ m-> exec ();
$ Parallel_time = microtime (true)-$ t;
Echo $ parallel_time. "\ n ";
$ T = microtime (true );
// Serial fetch (serial capture ):
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 php programming.

The example in this article analyzes the internal multithreading usage of PHP multithreading. Share it with you for your reference. Details: 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.