How does a phpcurl multi-threaded request allow each thread to output results immediately after the request is completed, instead of waiting for all requests to be output together?

Source: Internet
Author: User
The website root directory contains a test.html file with only a text demo. {Code ...} this is curl. php {code ...} 500 concurrent threads are set, so the result is output after about 10 seconds. The program does not respond during the waiting period. The result I want to achieve is that every time a curl thread completes the request...

The website root directory contains a test.html file with only a text demo.

    demo

This is curl. php


  
   
$ Ch) {curl_multi_add_handle ($ mh, $ ch) ;}$ running = null; do {curl_multi_exec ($ mh, $ running);} while ($ running> 0 ); // start multi-threaded foreach ($ chArr as $ k => $ ch) {$ result [$ k] = curl_multi_getcontent ($ ch ); // here the output content echo "$ result [$ k] \ n" curl_multi_remove_handle ($ mh, $ ch);} curl_multi_close ($ mh );
  

500 concurrent threads are set, so the result is output after about 10 seconds. The program does not respond during the waiting period.

The result I want to achieve is that every time a curl thread request is completed, a result is immediately output on the terminal: demon.

How can we implement it? If Php cannot be implemented, NodeJs and Python solutions are acceptable. thank you.

Reply content:

The website root directory contains a test.html file with only a text demo.

    demo

This is curl. php


  
   
$ Ch) {curl_multi_add_handle ($ mh, $ ch) ;}$ running = null; do {curl_multi_exec ($ mh, $ running);} while ($ running> 0 ); // start multi-threaded foreach ($ chArr as $ k => $ ch) {$ result [$ k] = curl_multi_getcontent ($ ch ); // here the output content echo "$ result [$ k] \ n" curl_multi_remove_handle ($ mh, $ ch);} curl_multi_close ($ mh );
  

500 concurrent threads are set, so the result is output after about 10 seconds. The program does not respond during the waiting period.

The result I want to achieve is that every time a curl thread request is completed, a result is immediately output on the terminal: demon.

How can we implement it? If Php cannot be implemented, NodeJs and Python solutions are acceptable. thank you.

Nodejs is asynchronous.

You can use ob_start () and ob_flush () to disable output_buffering. the default value is 4 K. If this parameter is not set, it will be invalid because you have too little content. If it is accessed by the browser, there will be no solution, because the browser has a buffer, too little will not be able to see the effect. Run the following command to test OK. if the page does not have the ini_set permission, go to php. ini and set output_buffering = 0.
By the way, the number of cycles is 500. The local php7 test is executed for 1 to 2 seconds, and php5.4 takes 28 seconds.


   $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);

Although it is multi-threaded, it is synchronous, not asynchronous. php does not seem to have the asynchronous capability.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.