Current Location: Home> Latest Articles> curl_multi_close best practices with multithreaded requests

curl_multi_close best practices with multithreaded requests

gitbox 2025-05-12

In PHP, making multithreaded requests is often implemented using the cURL library. cURL is a powerful library that can help us make HTTP requests. Using the curl_multi_* series functions, PHP can send multiple HTTP requests in parallel, thereby improving application performance. However, many people ignore the cleanup of resources after the request is over. In order to optimize the performance of multithreaded requests, the correct use of curl_multi_close function is particularly important.

What is curl_multi_close?

The curl_multi_close function is used to close cURL multi-handle initialized by curl_multi_init . Calling this function will free up cURL resources, avoid memory leaks, and ensure that the connection is properly closed after each parallel request is completed.

When making multi-threaded requests, each request may take some time to complete. To maximize performance and reduce memory consumption, curl_multi_close is a very important function.

How to optimize multithreaded request performance using curl_multi_close?

Here is a basic example of optimizing multithreaded request performance using curl_multi_close :

 <?php

// Create multiple requests URL Array
$urls = [
    "http://gitbox.net/api/data1",
    "http://gitbox.net/api/data2",
    "http://gitbox.net/api/data3"
];

// Initialize multiple cURL Handle
$multiHandle = curl_multi_init();
$curlHandles = [];

// Loop initializes each request
foreach ($urls as $url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  // Set the return content to a string
    curl_multi_add_handle($multiHandle, $ch);       // 添加到多线程Handle中
    $curlHandles[] = $ch;  // Save each cURL Handle
}

// Perform all requests
do {
    $status = curl_multi_exec($multiHandle, $active);
    if ($active) {
        // Wait for all requests to complete
        curl_multi_select($multiHandle);
    }
} while ($active);

// Get the results of all requests and clean up resources
foreach ($curlHandles as $ch) {
    $response = curl_multi_getcontent($ch);
    echo $response . "\n";  // Output request result

    // Remove cURL Handle
    curl_multi_remove_handle($multiHandle, $ch);

    // closure cURL Handle
    curl_close($ch);
}

// closure多重 cURL Handle
curl_multi_close($multiHandle);
?>

Explain the code

  1. Initialize multiple requests : We first define an array containing multiple URLs. Then, use curl_init to initialize a cURL handle for each URL.

  2. Add handle to multiple cURL handles : Through the curl_multi_add_handle function, we add each cURL handle to the multiple handles, so that multiple requests can be executed in parallel.

  3. Perform all requests : Use curl_multi_exec to perform all parallel requests. During the request process, use curl_multi_select to wait for the request to complete to ensure that we are not busy waiting.

  4. Clean up resources : After all requests are completed, we get the response data for each request through curl_multi_getcontent . Then, use curl_multi_remove_handle to remove each cURL handle from the multiple handles and call curl_close to close each handle.

  5. Close multiple handles : Finally, use curl_multi_close to close the multiple cURL handle. This step is to free up all resources to avoid memory leaks.

Best Practice: Optimize Parallel Requests

  1. Limit the number of parallel requests : Although PHP can make multithreaded requests, too many concurrent requests can cause excessive server load. Reasonably limiting the number of concurrency can help improve performance and avoid unnecessary network burdens.

  2. Error handling : In actual application, remember to check the return status of each request and handle the request failure. You can obtain detailed error information through curl_getinfo to ensure the stability of the program.

  3. Resource Release : Calling curl_multi_close and curl_close is a very important step, which can ensure that all resources are released. If you forget this step, it can lead to memory leaks, especially when making a lot of concurrent requests.

  4. Split of batch requests : If there are very large number of requests, consider splitting the request into multiple batches, each batch processing fewer requests. This can avoid performance bottlenecks caused by too large requests for a certain batch.

Summarize

By using curl_multi_close to optimize resource release for multi-threaded requests, the performance and stability of the application can be effectively improved. Correctly managing concurrent requests and resource cleaning can not only reduce memory leaks, but also improve program response speed.