A detailed _php tutorial using curl multithreading to simulate concurrency

Source: Internet
Author: User
First, let's look at the Curl multithreading function in PHP:
Copy CodeThe code is as follows:
# Curl_multi_add_handle
# Curl_multi_close
# curl_multi_exec
# curl_multi_getcontent
# Curl_multi_info_read
# Curl_multi_init
# Curl_multi_remove_handle
# Curl_multi_select

In general, when thinking about using these functions, it is clear that the purpose should be to request multiple URLs at the same time, instead of one by one, it is better to loop to tune Curl_exec.
The steps are summarized as follows:
First step: Call Curl_multi_init
Step two: Loop call Curl_multi_add_handle
It is important to note that the second parameter of Curl_multi_add_handle is a sub-handle from Curl_init.
Step three: Continue calling Curl_multi_exec
Fourth step: Loop call Curl_multi_getcontent as needed to get results
Fifth step: Call Curl_multi_remove_handle, and call curl_close for each word handle
Sixth step: Call Curl_multi_close
Here is an example of a simple online search, which the author calls the dirty, (I'll explain why Dirty later):
Copy CodeThe code is as follows:
/*
Here's a quick and dirty example for Curl-multi from PHP, tested on PHP 5.0.0rc1 Cli/freebsd 5.2.1
*/
$connomains = Array (
"http://www.cnn.com/",
"http://www.canada.com/",
"http://www.yahoo.com/"
);
$MH = Curl_multi_init ();
foreach ($connomains as $i = = $url) {
$conn [$i]=curl_init ($url);
curl_setopt ($conn [$i],curlopt_returntransfer,1);
Curl_multi_add_handle ($MH, $conn [$i]);
}
do {$n =curl_multi_exec ($MH, $active);} while ($active);
foreach ($connomains as $i = = $url) {
$res [$i]=curl_multi_getcontent ($conn [$i]);
Curl_close ($conn [$i]);
}
Print_r ($res);

The whole process is almost like this, but this simple code has a fatal weakness, that is, in the Do loop, during the entire URL request is a dead loop, it can easily lead to CPU consumption of 100%.

Now let's improve it, there is a function curl_multi_select with almost no documentation, although C's Curl Library has a description of select, but the interface and usage in PHP are different from C.
change the part of the above do to the following:
Copy CodeThe code is as follows:
do {
$MRC = Curl_multi_exec ($MH, $active);
} while ($MRC = = Curlm_call_multi_perform);
while ($active and $MRC = = CURLM_OK) {
if (Curl_multi_select ($MH)! =-1) {
do {
$MRC = Curl_multi_exec ($MH, $active);
} while ($MRC = = Curlm_call_multi_perform);
}
}

Because $active to wait for all the URL data to be completed before it becomes false, so here to use the Curl_multi_exec return value to determine whether there is data, when there is data when the call Curl_multi_exec, There is no data to enter the Select phase, and new data can be woken up and executed. The advantage here is that the CPU is useless.

In addition: there are some details that may sometimes be encountered:
Control the time-out for each request by curl_setopt before Curl_multi_add_handle:
curl_setopt ($ch, Curlopt_timeout, $timeout);
Determine if a time-out or other error was used before Curl_multi_getcontent: Curl_error ($conn [$i]);

Here I simply use the above example of the dirty (enough, not to find the CPU uses 100% of the situation).
Simulating concurrency for a "kandian.com" interface is to read and write data to Memcache. Because of the confidentiality of the relationship, the relevant data and results are not posted.

Simulated 3 times, the first 10 threads request 1000 times at the same time, the second, 100 threads request 1000 times at the same time, the third, 1000 threads simultaneously request 100 times (already quite laborious, dare not set more than 1000 multithreading).
It seems that Curl multithreading simulation concurrency still has some limitations.

It is also doubtful that a large error in the results caused by multithreading delays can be compared to data discovery. The time used to initialize and set is not very large, the difference is in the Get method, so it is easy to exclude this point ~ ~ ~

http://www.bkjia.com/PHPjc/327618.html www.bkjia.com true http://www.bkjia.com/PHPjc/327618.html techarticle first, learn about the Curl multithreading function in PHP: Copy Code as follows: # curl_multi_add_handle # curl_multi_close # curl_multi_exec # Curl_multi_getconte NT # curl_multi_i ...

  • Related Article

    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.