0

In test.html,demoword included only。

    demo

curl.php

<php?

//init curl
$chArr=[];
for($i=0;$i<500;$i++){
    $chArr[$i]=curl_init("http://dev.site/test.html");
    curl_setopt($chArr[$i],CURLOPT_RETURNTRANSFER,1);
}

//create curl
$mh = curl_multi_init(); 
foreach($chArr as $k => $ch){
    curl_multi_add_handle($mh,$ch); 
}

$running = null;

do{
    curl_multi_exec($mh,$running); 
}while($running > 0); 

//start multi-thread
foreach($chArr as $k => $ch){
    $result[$k]= curl_multi_getcontent($ch);  

    //and echo out the result
    echo "$result[$k]\n"      

    curl_multi_remove_handle($mh,$ch);
}

curl_multi_close($mh); 

I've set 500 requests,about 10s later got the output and there is no response during processing no response


Is there anyway to display the result and give me a Feedback since every single curl request is done,just like below?

enter image description here


PS:if PHP is hard to do this so,I could accept Nodejs,Python etc.

Chweng Mega
  • 1,598
  • 1
  • 13
  • 22

1 Answers1

0

You may need to use ob_start() and ob_end_flush().

ob_start() turns on the output buffering, so put that at the top of the for loop before echoing the output.

and

ob_end_flush() sends the output buffer and turn off output buffering.

So your code might look like this,

foreach($chArr as $k => $ch){
    ob_start();
    $result[$k]= curl_multi_getcontent($ch);  

    //and echo out the result
    echo "$result[$k]\n"      
    ob_end_flush();
    curl_multi_remove_handle($mh,$ch);
}
Alok Patel
  • 7,842
  • 5
  • 31
  • 47
  • sorry guy,I did like what said,however I still get the same thing,terminal only display the result when that all 500 curl were done,I exactly what php give me a feed back since every single curl request is completed – Chweng Mega Sep 05 '16 at 09:03