How can I use PHP call python script return result in realtime?

后端 未结 1 1175
走了就别回头了
走了就别回头了 2021-01-25 19:32

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

相关标签:
1条回答
  • 2021-01-25 20:28

    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)
    
    0 讨论(0)
提交回复
热议问题