First of all, let’s understand the curl multi-threading function in php:
Copy the code The code is as follows:
# curl_multi_add_handle
# curl_multi_close
# curl_multi_exec
# curl_multi_getcontent
# curl_multi_info_read
# curl_multi_remove_handle
# curl_multi_select
Generally speaking, when you think of using these functions, the purpose should obviously be to request multiple URLs at the same time, rather than requesting them one by one. Otherwise, it is better to adjust curl_exec in a loop yourself.
The steps are summarized as follows:
Step 1: Call curl_multi_init
Step 2: Call curl_multi_add_handle in a loop
What needs to be paid attention to in this step , the second parameter of curl_multi_add_handle is the subhandle from curl_init.
Step 3: Continue to call curl_multi_exec
Step 4: Call curl_multi_getcontent in a loop to obtain the results as needed
Step 5: Call curl_multi_remove_handle, and call curl_close for each word handle
Step 6: Call curl_multi_close
Here is a simple example found online. The author calls it a dirty example (I will explain why it is 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] ; A fatal weakness is the section of the do loop, which is an infinite loop during the entire url request. It can easily cause the CPU to occupy 100%.
Now let’s improve it. Here we need to use a function curl_multi_select that has almost no documentation. Although C’s curl library has instructions for select, the interface and usage in PHP are indeed different from those in C. .
Change the do paragraph above to the following:
Copy the code
The code is as follows:
do { $mrc = curl_multi_exec($mh,$active); ($active and $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { ;
}
Because $active has to wait until all url data is received before it becomes false, so the return value of curl_multi_exec is used here to determine whether there is still data. When there is data, curl_multi_exec will be called continuously. If there is no data temporarily, it will enter the select stage. , it can be awakened to continue execution as soon as new data comes. The advantage here is that there is no unnecessary consumption of CPU.
In addition: There are some details that may sometimes be encountered:
Control the timeout of each request, do it through curl_setopt before curl_multi_add_handle:
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
To determine whether it has timed out or other errors, use: curl_error($conn[$i]);
Here I Just simply use the above dirty example (it is enough, and I have not found 100% CPU usage).
Simulate concurrency on a certain interface of "Kandian.com". The function is to read data and write data to memcache. Due to confidentiality, relevant data and results will not be posted.
Simulated three times. The first time, 10 threads requested 1000 times at the same time. The second time, 100 threads requested 1000 times at the same time. The third time, 1000 threads requested 100 times at the same time (it is already quite strenuous, I dare not In settings with more than 1000 multithreads).
It seems that curl multi-threaded simulation concurrency still has certain limitations.
In addition, I also suspected that there may be a large error in the results due to multi-thread delay, and I found out by comparing the data. There is not much difference in the time taken for initialization and set. The difference lies in the get method, so this can be easily eliminated~~~
http://www.bkjia.com/PHPjc/327618.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327618.htmlTechArticleFirst of all, let’s understand the curl multi-thread function in php: Copy the code as follows: # curl_multi_add_handle # curl_multi_close # curl_multi_exec # curl_multi_getcontent # curl_multi_i...