Home Backend Development PHP Tutorial PHP concurrency uses curl concurrency to reduce backend access time

PHP concurrency uses curl concurrency to reduce backend access time

Apr 09, 2018 am 11:26 AM
curl php reduce

The content shared with you in this article is about using curl concurrency in PHP to reduce back-end access time. It has a certain reference value. Friends in need can refer to it


First of all, let’s understand the curl multi-threaded function in php:

# 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
Copy after login

Generally speaking, when you think of using these functions, the purpose should obviously be to request multiple URLs at the same time, rather than one by one. request, otherwise it is better to loop and adjust curl_exec yourself.

The steps are summarized as follows:

The first step: Call curl_multi_init
The second step: Call curl_multi_add_handle in a loop
It should be noted in this step that the second parameter of curl_multi_add_handle is given by Subhandle from curl_init.
Step 3: Continuously call curl_multi_exec
Step 4: Cyclically call curl_multi_getcontent 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 dirty later):

/*
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);
Copy after login



The entire usage process is almost like this. However, this simple code has a fatal weakness, that is, the do loop section is an infinite loop during the entire url request, which 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 section above to the following:
           

  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);
                        }
                }
Copy after login


Because $active has to wait until all url data is received before it becomes false, so curl_multi_exec is used here The return value determines whether there is still data. When there is data, curl_multi_exec is called continuously. If there is no data for the time being, it enters the select stage. Once new data comes, it can be awakened to continue execution. 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 there is a timeout or other errors, use curl_error($conn[$i]);

before curl_multi_getcontent: curl_error($conn[$i]);

#Here I simply use the dirty example mentioned above (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 and write data to memcache. Due to confidentiality, relevant data and results will not be posted.

I simulated 3 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, we also suspect that there may be a large error in the results due to multi-thread delay, and we 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 ://log.dongsheng.org/2008/07/16/curl-multiple-handlers/

Usually, cURL in PHP runs blocking, which means that after creating a cURL request, you must wait for it to execute. The next request will be executed only after success or timeout. The curl_multi_* series of functions make successful concurrent access possible. The PHP documentation does not introduce this function in detail. The usage is as follows:

###



源码打印?

$requests = array('http://www.baidu.com', 'http://www.google.com');  
$main    = curl_multi_init();  
$results = array();  
$errors  = array();  
$info = array();  
$count = count($requests);  
for($i = 0; $i < $count; $i++)   
{    
$handles[$i] = curl_init($requests[$i]);    
var_dump($requests[$i]);    
curl_setopt($handles[$i], CURLOPT_URL, $requests[$i]);    
curl_setopt($handles[$i], CURLOPT_RETURNTRANSFER, 1);    
curl_multi_add_handle($main, $handles[$i]);  
}  
$running = 0;   
do {    
curl_multi_exec($main, $running);  
}   
while($running > 0);   
for($i = 0; $i < $count; $i++)  
{  $results[] = curl_multi_getcontent($handles[$i]);    
$errors[]  = curl_error($handles[$i]);    
$info[]    = curl_getinfo($handles[$i]);    
curl_multi_remove_handle($main, $handles[$i]);  
}  
curl_multi_close($main);  
var_dump($results);  
var_dump($errors);  
var_dump($info);
Copy after login


http://www.searchtb.com/2010/12/using-multicurl-to-improve-performance.html

前言:在我们平时的程序中难免出现同时访问几个接口的情况,平时我们用curl进行访问的时候,一般都是单个、顺序访问,假如有3个接口,每个接口耗时500毫秒那么我们三个接口就要花费1500毫秒了,这个问题太头疼了严重影响了页面访问速度,有没有可能并发访问来提高速度呢?今天就简单的说一下,利用curl并发来提高页面访问速度,希望大家多指导。1、老的curl访问方式以及耗时统计




源码打印?

<?php function curl_fetch($url, $timeout=3){       
$ch = curl_init();       
curl_setopt($ch, CURLOPT_URL, $url);       
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);       
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);       
$data = curl_exec($ch);       
$errno = curl_errno($ch);       
if ($errno>0) {           
$data = false;       
}       
curl_close($ch);       
return $data;   
}   
function microtime_float()   
{      
list($usec, $sec) = explode(" ", microtime());      
return ((float)$usec + (float)$sec);   
}   
$url_arr=array(        
"taobao"=>"http://www.taobao.com",        
"sohu"=>"http://www.sohu.com",        
"sina"=>"http://www.sina.com.cn",        
);    
$time_start = microtime_float();    
$data=array();    
foreach ($url_arr as $key=>$val)    
{        
$data[$key]=curl_fetch($val);    
}    
$time_end = microtime_float();    
$time = $time_end - $time_start;    
echo "耗时:{$time}";   
?>
Copy after login

耗时:0.614秒
2、curl并发访问方式以及耗时统计




源码打印?

<?php   
function curl_multi_fetch($urlarr=array()){       
$result=$res=$ch=array();       
$nch = 0;       
$mh = curl_multi_init();       
foreach ($urlarr as $nk => $url) {           
$timeout=2;           
$ch[$nch] = curl_init();           
curl_setopt_array($ch[$nch], array(           
CURLOPT_URL => $url,           
CURLOPT_HEADER => false,           
CURLOPT_RETURNTRANSFER => true,           
CURLOPT_TIMEOUT => $timeout,           
));           
curl_multi_add_handle($mh, $ch[$nch]);           
++$nch;     }       
/* wait for performing request */      
do {           
$mrc = curl_multi_exec($mh, $running);       
} while (CURLM_CALL_MULTI_PERFORM == $mrc);         
while ($running && $mrc == CURLM_OK) {           
// wait for network           
if (curl_multi_select($mh, 0.5) > -1) {               
// pull in new data;               
do {                   
$mrc = curl_multi_exec($mh, $running);               
} while (CURLM_CALL_MULTI_PERFORM == $mrc);           
}       
}         
if ($mrc != CURLM_OK) {           
error_log("CURL Data Error");       
}         
/* get data */      
$nch = 0;       
foreach ($urlarr as $moudle=>$node) {           
if (($err = curl_error($ch[$nch])) == &#39;&#39;) {               
$res[$nch]=curl_multi_getcontent($ch[$nch]);             $result[$moudle]=$res[$nch];         }           
else          
{               
error_log("curl error");           
}           
curl_multi_remove_handle($mh,$ch[$nch]);           
curl_close($ch[$nch]);           
++$nch;       
}       
curl_multi_close($mh);       
return  $result;   
}   
$url_arr=array(        
"taobao"=>"http://www.taobao.com",        
"sohu"=>"http://www.sohu.com",        
"sina"=>"http://www.sina.com.cn",        
);   
function microtime_float()   
{      
list($usec, $sec) = explode(" ", microtime());      
return ((float)$usec + (float)$sec);   
}   
$time_start = microtime_float();   
$data=curl_multi_fetch($url_arr);   
$time_end = microtime_float();   
$time = $time_end - $time_start;   
echo "耗时:{$time}";   
?>
Copy after login

耗时:0.316秒
帅气吧整个页面访问后端接口的时间节省了一半
3、curl相关参数
来自:http://cn2.php.net/manual/en/ref.curl.php
curl_close — Close a cURL session
curl_copy_handle — Copy a cURL handle along with all of its preferences
curl_errno — Return the last error number
curl_error — Return a string containing the last error for the current session
curl_exec — Perform a cURL session
curl_getinfo — Get information regarding a specific transfer
curl_init — Initialize a cURL session
curl_multi_add_handle — Add a normal cURL handle to a cURL multi handle
curl_multi_close — Close a set of cURL handles
curl_multi_exec — Run the sub-connections of the current cURL handle
curl_multi_getcontent — Return the content of a cURL handle if CURLOPT_RETURNTRANSFER is set
curl_multi_info_read — Get information about the current transfers
curl_multi_init — Returns a new cURL multi handle
curl_multi_remove_handle — Remove a multi handle from a set of cURL handles
curl_multi_select — Wait for activity on any curl_multi connection
curl_setopt_array — Set multiple options for a cURL transfer
curl_setopt — Set an option for a cURL transfer
curl_version — Gets cURL version information

前端开发中的性能那点事(三)php的opcode缓存

前端开发中的性能那点事(一)巧用xdebug

 

http://www.oschina.net/question/234345_42245

 相关推荐:

php并发之关于查询MySQL的示例(图)

PHP并发读写文件如何解决

The above is the detailed content of PHP concurrency uses curl concurrency to reduce backend access time. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

See all articles