There is a test.html in the root directory of the website, and the content is only text demo.
<code> demo </code>
This is curl.php
<code><php? //设置curl $chArr=[]; for($i=0;$i<500;$i++){ $chArr[$i]=curl_init("http://dev.site/test.html"); curl_setopt($chArr[$i],CURLOPT_RETURNTRANSFER,1); } //创建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); //启动多线程 foreach($chArr as $k => $ch){ $result[$k]= curl_multi_getcontent($ch); //在这里输出内容 echo "$result[$k]\n" curl_multi_remove_handle($mh,$ch); } curl_multi_close($mh); </code>
The concurrency number is set to 500, so the result is output after waiting for about 10 seconds. During the waiting period, the program does not respond.
The effect I want to achieve is that whenever a curl thread request is completed, a result can be output in the terminal immediately: demon.
How should you implement it? If Php cannot achieve it, NodeJs and Python solutions are acceptable, thank you
There is a test.html in the root directory of the website, and the content is only text demo.
<code> demo </code>
This is curl.php
<code><php? //设置curl $chArr=[]; for($i=0;$i<500;$i++){ $chArr[$i]=curl_init("http://dev.site/test.html"); curl_setopt($chArr[$i],CURLOPT_RETURNTRANSFER,1); } //创建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); //启动多线程 foreach($chArr as $k => $ch){ $result[$k]= curl_multi_getcontent($ch); //在这里输出内容 echo "$result[$k]\n" curl_multi_remove_handle($mh,$ch); } curl_multi_close($mh); </code>
The concurrency number is set to 500, so the result is output after waiting for about 10 seconds. During the waiting period, the program does not respond.
The effect I want to achieve is that whenever a curl thread request is completed, a result can be output in the terminal immediately: demon.
How should you implement it? If Php cannot achieve it, NodeJs and Python solutions are acceptable, thank you
nodejs is asynchronous
You can use ob_start() and ob_flush(), provided that output_buffering is turned off. The default size is 4k. If not, it will be invalid because you have too little content. If it is accessed through a browser, there is no solution, because the browser still has a buffer, and if it is too small, the effect will not be seen. The following test is OK on the command line. If the page does not have ini_set permission, go directly to php.ini and set output_buffering=0
By the way, the loop is 500 times. The local php7 test executes for 1 to 2 seconds, and php5.4 takes 28 seconds.
<code class="php"><?php ini_set('output_buffering', 0); ob_start(); $chArr=[]; for($i=0;$i<500;$i++){ $chArr[$i]=curl_init("http://dev.site/test.html"); curl_setopt($chArr[$i],CURLOPT_RETURNTRANSFER,1); } $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); foreach($chArr as $k => $ch){ $result[$k]= curl_multi_getcontent($ch); echo "$result[$k]\n"; ob_flush(); flush(); sleep(1); curl_multi_remove_handle($mh,$ch); } ob_end_flush(); curl_multi_close($mh);</code>
Although it is multi-threaded, it is synchronous, not asynchronous. It seems that PHP does not have asynchronous capabilities