代码之家  ›  专栏  ›  技术社区  ›  Matei Zoc

PHP curl_multiexec apache限制?

  •  0
  • Matei Zoc  · 技术社区  · 5 年前

    我有这个PHP代码:

    test2.php

    ini_set('max_execution_time', '900');
    ini_set('memory_limit', '2048M');
    $start = microtime(true);
    
    $mh = curl_multi_init();
    $handles = array();
    
    // create several requests
    for ($i = 0; $i < 50; $i++) {
        $ch = curl_init();
    
        $rand = rand(5,25); // just making up data to pass to script
        curl_setopt($ch, CURLOPT_URL, "http://example.com/test-3.php?time=$rand");
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    
        curl_multi_add_handle($mh, $ch);
        $handles[] = $ch;
    }
    
    // execute requests and poll periodically until all have completed
    $isRunning = null;
    do {
        curl_multi_exec($mh, $isRunning);
    } while ($isRunning > 0);
    
    // fetch output of each request
    $outputs = array();
    for ($i = 0; $i < count($handles); $i++) {
        $outputs[$i] = trim(curl_multi_getcontent($handles[$i]));
        curl_multi_remove_handle($mh, $handles[$i]);
    }
    
    curl_multi_close($mh);
    
    print_r('<pre>');
    print_r($outputs);
    print_r('</pre>');
    
    printf("Elapsed time: %.2f seconds\n", microtime(true) - $start);
    

    test3.php

     sleep(5);
     echo time();
    

    每次我访问test-2.php时,通过URL(apache)最多只能接收10个输出

    Array
    (
        [0] => 1599904635
        [1] => 1599904635
        [2] => 1599904635
        [3] => 1599904635
        [4] => 1599904635
        [5] => 1599904635
        [6] => 1599904635
        [7] => 1599904635
        [8] => 1599904635
        [9] => 1599904635
        [10] => 
        [11] => 
        [12] => 
        [13] => 
        [14] => 
        [15] => 
        [16] => 
        [17] => 
        [18] => 
        [19] => 
        [20] => 
        [21] => 
        [22] => 
        [23] => 
        [24] => 
        [25] => 
        [26] => 
        [27] => 
        [28] => 
        [29] => 
        [30] => 
        [31] => 
        [32] => 
        [33] => 
        [34] => 
        [35] => 
        [36] => 
        [37] => 
        [38] => 
        [39] => 
        [40] => 
        [41] => 
        [42] => 
        [43] => 
        [44] => 
        [45] => 
        [46] => 
        [47] => 
        [48] => 
        [49] => 
    )
    
    Elapsed time: 5.02 seconds 
    

    但如果我运行test4.php中的代码

    exec('php /test-2.php',$output);
    print_r($output);
    

    正在显示所有50。。。但每次我尝试通过URL运行时,最多只能运行10。。。有什么办法可以提高限额吗?

    0 回复  |  直到 5 年前
    推荐文章