dongyi1015 2009-02-26 21:47
浏览 76
已采纳

提高cURL性能(PHP库)

Here is a brief overview of what I am doing, it is quite simple really:

  • Go out and fetch records from a database table.
  • Walk through all those records and for each column that contains a URL go out (using cURL) and make sure the URL is still valid.
  • For each record a column is updated with a current time stamp indicating when it was last checked and some other db processing takes place.

Anyhow all this works well and good and does exactly what it is supposed to. The problem is that I think performance could be greatly improved in terms of how I am validating the URL's with cURL.

Here is a brief (over simplified) excerpt from my code which demonstrates how cURL is being used:

$ch = curl_init();
while($dbo = pg_fetch_object($dbres))
{
   // for each iteration set url to db record url
   curl_setopt($ch, CURLOPT_URL, $dbo->url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_exec($ch); // perform a cURL session
   $ihttp_code = intval(curl_getinfo($ch, CURLINFO_HTTP_CODE));
   // do checks on $ihttp_code and update db
}
// do other stuff here
curl_close($ch);

As you can see I am just reusing the same cURL handle the entire time but even if I strip out all over the processing (database or otherwise) the script still takes incredibly long to run. Would changing any of the cURL options help improve performance? Tuning timeout values / etc? Any input would be appreciated.

Thank you,

  • Nicholas
  • 写回答

1条回答 默认 最新

  • dsymx68408 2009-02-26 22:29
    关注

    Set CURLOPT_NOBODY to 1 (see curl documentation) tell curl not to ask for the body of the response. This will contact the web server and issue a HEAD request. The response code will tell you if the URL is valid or not, and won't transfer the bulk of the data back.

    If that's still too slow, then you'll likely see a vast improvement by running N threads (or processes) each doing 1/Nth of the work. The bottleneck may not be in your code, but in the response times of the remote servers. If they're slow to respond, then your loop will be slow to run.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么