When using CURL in PHP, the curl_multi_* function is often used to execute multiple CURL requests simultaneously. To ensure efficient code execution, it is important to properly close multithreaded CURL sessions, especially when calling the curl_multi_close function. This article will introduce how to use curl_multi_close correctly to ensure that resources are thoroughly cleaned and avoid memory leaks and file handle leaks.
In PHP, the curl_multi_* function is used to manage multiple CURL sessions in parallel. curl_multi_close is one of the very important functions that are used to close multithreaded CURL sessions and free up related resources. Its basic function is to destroy all CURL session resources created with curl_multi_init to avoid resource leakage.
Initialize multiple CURL sessions
First, you need to initialize multiple CURL sessions with curl_init and add them to a curl_multi_handle . The curl_multi_init function is used to create a CURL multi-handle that will manage all subsessions.
$multi_handle = curl_multi_init();
$ch1 = curl_init("https://gitbox.net/api/resource1");
$ch2 = curl_init("https://gitbox.net/api/resource2");
curl_multi_add_handle($multi_handle, $ch1);
curl_multi_add_handle($multi_handle, $ch2);
Perform multiple requests
Use curl_multi_exec to execute these requests in parallel. The function will be executed until all requests are completed.
do {
$mrc = curl_multi_exec($multi_handle, $active);
} while ($active);
Close multiple handles
After all CURL requests are completed, you need to call curl_multi_close to close the multiple handles and release all related resources. Make sure that each handle has been closed to prevent memory leaks.
curl_multi_remove_handle($multi_handle, $ch1);
curl_multi_remove_handle($multi_handle, $ch2);
curl_multi_close($multi_handle);
Calling curl_multi_close is very important because it ensures that all resources allocated during multithreaded requests can be properly released. If curl_multi_close is not called, it may cause resource leakage, especially in long-term scripts or services, system resources will be continuously consumed, thereby reducing system performance.
Close each individual CURL handle
Before closing multiple handles, you should first remove all the clone handles through curl_multi_remove_handle , and then call curl_multi_close .
curl_multi_remove_handle($multi_handle, $ch1);
curl_multi_remove_handle($multi_handle, $ch2);
curl_close($ch1);
curl_close($ch2);
Make sure there are no active sessions
Before calling curl_multi_close , make sure that all requests have been completed, and the return status of curl_multi_exec should be CURLM_OK . This avoids attempting to close the session when the request is not completed.
do {
$mrc = curl_multi_exec($multi_handle, $active);
} while ($active);
Regularly check resources
During development, it is important to check regularly whether CURL resources are released correctly, especially in loops or when handling large numbers of concurrent requests. If you encounter excessive memory consumption, you can use functions such as memory_get_usage() to troubleshoot.
Correct use of curl_multi_close is an important step to ensure that PHP applications run efficiently and memory-free. By reasonably managing the creation and destruction of CURL sessions, resource leakage problems can be effectively avoided. Especially when handling multiple concurrent requests, correctly closing multiple handles can significantly improve system stability and performance.