Home > Backend Development > PHP Tutorial > How to use php curl multi-threaded request to make each thread output the results immediately after completing the request, instead of waiting for all requests to be completed and output them together?

How to use php curl multi-threaded request to make each thread output the results immediately after completing the request, instead of waiting for all requests to be completed and output them together?

WBOY
Release: 2016-09-06 08:57:09
Original
1436 people have browsed it

There is a test.html in the root directory of the website, and the content is only text demo.

<code>    demo
</code>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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.
How to use php curl multi-threaded request to make each thread output the results immediately after completing the request, instead of waiting for all requests to be completed and output them together?

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 to use php curl multi-threaded request to make each thread output the results immediately after completing the request, instead of waiting for all requests to be completed and output them together?

How should you implement it? If Php cannot achieve it, NodeJs and Python solutions are acceptable, thank you

Reply content:

There is a test.html in the root directory of the website, and the content is only text demo.

<code>    demo
</code>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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.
How to use php curl multi-threaded request to make each thread output the results immediately after completing the request, instead of waiting for all requests to be completed and output them together?

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 to use php curl multi-threaded request to make each thread output the results immediately after completing the request, instead of waiting for all requests to be completed and output them together?

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>
Copy after login

Although it is multi-threaded, it is synchronous, not asynchronous. It seems that PHP does not have asynchronous capabilities

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template