Print Output as it is generated in PHP - php

Here is the pseudo code of what I am doing:
<?php
for ($x=0;$x<5;$x++)
{
$result=shell_exec("python code.py");
echo $result;
sleep(15);
}
?>
Here is the code.py pseudo code
try:
condition
print "successful"
except:
print "not successful"
What I am trying to do is, each time the python code is run print its output on the web page instantly, then initiate the sleep timer and repeat.
Instead what I am getting now is the web page stalls for a cumulative 15*5 secs, and displays output of the python code that was run 5 times in one go.
Any suggestions on how this can be done

Related

Running A PHP File In Console And Clearing Echoed Results In A Loop?

I have a PHP file that currently runs via my console on my Mac. It's a private piece of software that interacts with an API via a loop that runs every x seconds.
At the minute Im am echoing out all information during each loop but the problem is within console it just repeats the content and appends it on to the previous. Is there a way of clearing all previous echoed results after each loop?
I thought I could use \r to replace the line but that doesn't seem to be working. I have also tried a few other examples after searching on here without any luck.
while(1){
echo "Some info goes here";
sleep(5);
}
For example if I try the following in a while loop
while(1){
echo "==============\r\n";
echo "==============\r\n";
sleep(5);
}
I end up with after a hand full of loops
Thanks
Two ways that I can think of off the top of my head:
Use the ANSI escape sequence to clear the screen:
print chr(27) . "[2J" . chr(27) . "[;H";
Run the command that clears the screen for your platform:
system("cls"); for windows
system("clear"); for unix-like systems
echo "\033[2J\033[;H";
Should clear your terminal.
If you just want to overwrite the same line again, this is working for me:
<?php
while(1) {
echo "test\r";
sleep(1);
}
?>

Don't wait for response and print the result immediately

I have a function sendSMSMessage() and it runs after printing some results:
<?php
...
print $x;
sendSMSMessage(params);
...
?>
This process of this function might take too long time of running and the app has to wait a long time before getting a response so i would like to print the results regardless of whether the sendSMSMessage has done...

After the function is done, sleep and print text

Hello, I have a question about PHP sleep().
This is my source code:
if (function here) {
functions.
echo 'create ok';
}
sleep(10);
if (function here) {
functions.
echo 'Move Finished';
}
when I want to run this functions, the system sleeps for 10 seconds and after that shows:
create ok
Move Finished
But what I want is that when the function is done, show and print (create ok), after that sleep for 10 seconds and after that show and print (Move Finished).
What should I do?
If you're using this code on a web page, you will need to flush the output buffer, i.e.
echo "create ok\n";
flush();
sleep(10);
echo "move finished\n";
See also: flush()

Sleep() Function not doing what I think it should

First, correct me if i'm wrong but i'm under the assumption that when you run the Sleep() function, it pauses running the script where it is located in the script, not at the beginning. If that is true can someone tell me why the below script waits 5 seconds and then shows both echos at the same time. NOT echo the first statement on page load and then wait 5 seconds and then fire the second echo....
echo "Your account username has been updated, you will now be redirected to the home page!";
sleep(5);
echo "REDIRECT!";
In your code PHP execution will pause for 5 seconds but it will not render itself part by part. i.e. It will not show the first statement and then the second. PHP keeps all its value in output buffer and display them when its finishes execution.
What happens is, it holds the value of first echo in output buffer and then waits for 5 seconds, then is holds another echo output in output buffer and shows all at once.
What you are trying to do is a lot easier in JS.
echo "Your account username has been updated, you will now be redirected to the home page!";
echo "<script> document.setTimeout(function() { document.location('redirect.html'); }, 5000); </script>";

PHP Sleep Output before putting to sleep for next step

I am using SLEEP(). It seems the function works, but the script waits until all my sleep() functions have ran their course before the script displays any output.
Is there a way I can get output for for 1st and then it should wait and go to next ?
echo date('h:i:s') . "<br />";
//sleep for 10 seconds
sleep(10);
//start again
echo date('h:i:s'). "<br />";
//sleep for 10 seconds
sleep(10);
//start again
echo date('h:i:s');
I need output of 1st echo and then script should wait and give second echo ....and so on
Sleep Does not matter as for as output of the page is concerned. Page is served as soon as the last sleep is done... Though it stop processing of the page on server for the time of the sleep. .. in your scenario you should be using javascript for the affect.
You see, PHP runs completely on server-side, and sleep will just make the latency of the page more severe.
As Rab has suggested, try using JavaScript instead. It's not the same as Java (in fact, a lot different), and you might like to find some good tutorials online, or get some good books (the one from Murach is really good, trust me).

Categories