dongtiao2105 2013-07-03 09:56
浏览 9
已采纳

Zend Framework 1.12中的Http客户端太慢了

I want to send ~50 requests to different pages on the same domain and then, I'm using DOM object to gain urls to articles.

The problem is that this number of requests takes over 30 sec.

for ($i = 1; $i < 51; $i++)
{
    $url = 'http://example.com/page/'.$i.'/';             

    $client = new Zend_Http_Client($url);
    $response = $client->request();
    $dom = new Zend_Dom_Query($response); // without this two lines, execution is also too long
    $results = $dom->query('li');         //
}

Is there any way to speed this up?

  • 写回答

2条回答 默认 最新

  • doujiunai2169 2013-07-04 09:40
    关注

    It's a generel problem by design - not the code itself. If you're doing a for-loop over 50 items each opening an request to an remote uri, things get pretty slow since every requests waits until responde from the remote uri. e.g.: a request takes ~0,6 sec to been completed, multiple this by 50 and you get an exection time of 30 seconds!

    Other problem is that most webserver limits its (open) connections per client to an specific amount. So even if you're able to do 50 requests simultaneously (which you're currently not), things won't speed up measurably.

    In my option there is only one solution (without any deep going changes): Change the amout of requests per exection. Make chunks from e.g. only 5 - 10 per (script)-call and trigger them by an external call (e.g. run them by cron).

    Todo: Build a wrapper function which is able to save the state of its current run ("i did request 1 - 10 at my last run, so now I have to call 11 - 20) into a file or database and trigger this function by an cron.

    Example Code (untested) for better declaration;

    [...]
    
    private static $_chunks = 10; //amout of calls per run
    
    public function cronAction() {
    
        $lastrun = //here get last run parameter saved from local file or database
    
        $this->crawl($lastrun);
    
    }
    
    private function crawl($lastrun) {
    
        $limit = $this->_chunks + $lastrun;
    
        for ($i = $lastrun; $i < limit; $i++)
        {
            [...] //do stuff here
        }
    
        //here set $lastrun parameter to new value inside local file / database
    
    }
    
    [...]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)