curl-and-php
RE: curl_multi and cpu time issues.
Date: Wed, 6 Dec 2006 13:18:01 -0500
I can claim absolutely no credit for this (I grabbed this code from the
web somewhere, forgot where), but this version addresses the CPU use
time:
for ($i=0;$i<$chc;$i++) {
curl_setopt($chh[$i],CURLOPT_URL,$chu[$i]);
curl_multi_add_handle ($mh,$chh[$i]);
}
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);
}
}
if ($mrc != CURLM_OK) {
print "Curl multi read error $mrc\n";
}
I've never tried to completely understand it, but this one does not eat
CPU time.
Rob
-----Original Message-----
From: curl-and-php-bounces_at_cool.haxx.se
[mailto:curl-and-php-bounces_at_cool.haxx.se] On Behalf Of Brian Graves
Sent: Wednesday, December 06, 2006 12:15 PM
To: curl-and-php_at_cool.haxx.se
Subject: curl_multi and cpu time issues.
I have a PHP script that used to run on regular curl requests and
decided to switch to curl_multi for the speed benefits. With the script
only running for a few hours at the end of the day, my cpu time reports
from averaging around 45 minutes to 6 hours yesterday.
This is the only thing I changed in the last 24 hours, so I know the
curl_multi is the culprit. I can also see sharp jumps in processor time
right after I switched to curl_multi
Here's the code: (my script pulls from 5 sites)
$urlArray[0] = "http://website.com/";
$urlArray[1] = "http://anotherwebsite.com/"; $urlArray[2] =
"http://andanother.com/"; $urlArray[3] = "http://onemore.com/";
$urlArray[4] = "http://lastone.com/";
$mh = curl_multi_init();
foreach ($urlArray 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 ($urlArray as $i => $url) {
$res[$i]=curl_multi_getcontent($conn[$i]);
curl_close($conn[$i]);
}
Perhaps there's something wrong with my code? It seems to be in order.
What's the problem?
Thanks,
-Brian
PHP Version: 5.1.2
CURL Information libcurl/7.13.2 OpenSSL/0.9.7e zlib/1.2.2 libidn/0.5.13
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-php
Received on 2006-12-06