doumaogui5937 2012-10-06 12:21 采纳率: 100%
浏览 95

PHP中的非阻塞卷曲请求

So I have this function for making non-blocking curl requests. It works fine on what I've tested so far (small amounts of requests). But I need this to scale up to thousands of requests (maybe max 10,000). My issue is that I don't want to run into issues with too many parallel requests running at once.

What would you suggest to rate-limit the requests? Usleep? Requests in batches? The function is below:

function poly_curl($requests){

            $queue = curl_multi_init(); 
            $curl_array = array(); 
            $count = 0;
            foreach($requests as $request) 
            { 
                $curl_array[$count] = curl_init($request); 
                curl_setopt($curl_array[$count], CURLOPT_RETURNTRANSFER, true); 
                curl_multi_add_handle($queue, $curl_array[$count]); 
                $count++;
            } 

            $running = NULL; 
            do { 

               curl_multi_exec($queue,$running); 

            } while($running > 0); 

            $res = array(); 
            $count = 0;
            foreach($requests as $request) 
            { 
                $res[$count] = curl_multi_getcontent($curl_array[$count]); 
                $count++;
            } 

            $count = 0;
            foreach($requests as $request){ 
                curl_multi_remove_handle($queue, $curl_array[$count]); 
                $count++;
            } 
            curl_multi_close($queue);        
            return $res; 
    }
  • 写回答

4条回答 默认 最新

  • duangang4940 2012-10-06 12:38
    关注

    You may want to implement something that is called Exponential Backoff (wikipedia).

    Basically, it is an algorithm that allows you dynamically scale your processes depending on some feedback.

    You define a rate in your application, and on the first time-out, error, or anything you decide, you decrease this rate until the request finishes.

    You may implement it easily using the HTTP response code for example.

    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答