Skip to content Skip to sidebar Skip to footer

How Can I Use Php Call Python Script Return Result In Realtime?

I used PHP to call python script successfully and got the result . But I have to wait for the end of script running without anything output. It looks not friendly to my customer. H

Solution 1:

By specification, exec stop the calling program until the end of the callee. After that, you get back the output in a variable.

If you want to send data as soon as they are produced, you should use popen. It will fork a new process, but will not block the caller. So you can perform other tasks, like looping to read the sub-process output line by line to send it to your client. Something like that:

$handle = popen("python ./some.py ", 'r');
while(!feof($handle)) {
    $buffer = fgets($handle);
    echo"$buffer<br/>\n";
    ob_flush();
}
pclose($handle)

Post a Comment for "How Can I Use Php Call Python Script Return Result In Realtime?"