php curl_multi_exec() taking longer than time set in CURLOPT_TIMEOUT_MS

392 views Asked by At

I am using curl_multi_exec() to get data from some APIs and using the following code :

        $startTime = microtime(true);
        $running = null; 
        do
        {
          curl_multi_exec ( $curlMultiHandleResource, $running );
          usleep(50000);
        }
        while ( $running > 0 );

        $runningTime = microtime(true)-$startTime;

and I have set CURLOPT_TIMEOUT_MS = 1800 and CURLOPT_CONNECTTIMEOUT_MS = 1800. On my windows machine $runningTime is always around 1.8 secs or lesser but on linux machine it sometimes comes out to be even more than 2.5 secs. So I wanted to know why is this taking longer than CURLOPT_TIMEOUT_MS?

EDIT : I narrowed down the problem somewhat. I echoed the time taken in the call to curl_multi_exec() and found that every time in the second iteration of while loop it is taking longer than usual. Usually it is taking 10^-3 to 10^-5 seconds but in second iteration it is taking 0.5 to 1.5 seconds.

1

There are 1 answers

1
saurabh verma On

After searching a lot I found that the AsynchDNS was not enabled on my Linux machine and hence it was sequentially resolving the domains in the urls and hence taking a lot of time. After updating my libcurl library to 7.21.7 and enabling the AsynchDNS it worked as expected.