Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

printing process output in realtime

Tags:

php

apache

I am using PHP 5.3.4 with Apache 2.2.17 on a Windows 7 x64 system. I would like to have my PHP page output the result of a system call in real-time to the user's browser. To that end, I have configured output_buffering=Off in php.ini and created this code:

<?php
ob_implicit_flush(true);
ob_end_flush();
system('ping -n 10 www.google.com');
?>

The result of the ping is printed in real-time, but I also get a PHP diagnostic error and callstack at the top of my page that says:

Notice: ob_end_flush() [ref.outcontrol]: failed to delete and flush buffer. No buffer to delete or flush in index.php on line 3

What do I need to do to either correct or suppress this error?

Update If I change ob_end_flush() to $a = 1/0; I get a similar error and the output is realtime in all browsers. Is it something about the way the exception is printed that causes this to work?

like image 983
PaulH Avatar asked Jan 06 '12 22:01

PaulH


People also ask

How do I get subprocess to Popen output?

popen. To run a process and read all of its output, set the stdout value to PIPE and call communicate(). The above script will wait for the process to complete and then it will display the output.

How do you get continuous output in Python?

Use While loop with True condition expression to take continuous input in Python. And break the loop using if statement and break statement.

What is subprocess Popen?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.

What is subprocess Check_output?

The subprocess. check_output() is used to get the output of the calling program in python. It has 5 arguments; args, stdin, stderr, shell, universal_newlines. The args argument holds the commands that are to be passed as a string.


2 Answers

some web browsers buffer the first x bytes before they start to render a page, under certain conditions.

try just outputting lots of whitespace first

like image 200
goat Avatar answered Sep 27 '22 21:09

goat


I have a solution that works, but it is non-performant and icky. I throw an exception, but hide the exception dialog.

<?php
    ob_implicit_flush(true);

    // Something about the way exceptions are thrown causes Firefox and Chrome 
    // to be able to display the results of the system call in real-time rather
    // than having to wait for the call to complete. So, I just hide the 
    // exception message. IE9 works with or without this.
    echo "<div style=\"display:none\">";
    $a = 1/0;
    echo "</div>";

    echo "<pre>";
    system('ping -n 5 www.google.com');
    echo "</pre>";
?>

To auto-scroll to the bottom of the page, I add some javascript:

<html><head>
<script language="javascript">
var int = self.setInterval("window.scrollBy(0,1000);", 200);
</script>
</head>
<body>
<?php
    // insert above php code here
    // stop scrolling when the execution finishes
    echo '<script language="javascript">int = window.clearInterval(int);</script>';
?>
</body>
</html>

EDIT

@Chris's answer shows a much better solution.

echo '<div style="display:none">';
for ($a = 0; $a < 768; $a++)
    echo ' ';
echo '</div>';
like image 21
PaulH Avatar answered Sep 27 '22 21:09

PaulH