trouble with sleep()

Hi there. My (probably badly written code) is trying to read entries from a CSV file and, based on those entries decide if a script should be execute and, if so, how long to pause before execution. The script itself works - the data is read correctly into the array and I can check (echo) the fields without issue.

However, when I try to SLEEP() based on the the value from the file the script appears to JUST SLEEP without executing the echo commands. What I should see is a series of information about my csv file and then a sleep and then some more information. What I do see is just a long wait, then all the information at once.

I’ve tried using a static value in the sleep function (sleep(10)) but the same thing happens. It sleeps, then echos the information rather than echo/sleep/echo/sleep in a while loop.

For references $data[0] is a username, $data[1] is a password, $data[2] is whether or not to process the given entry, $data[3] is a % chance to execute the script and $data[4] is the delay (in seconds) before execution.

Any thoughts?

[php]<?php
$row = 1;
if (($handle = fopen(“my_test_file.csv”, “r”)) !== FALSE) {
while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE) {
$num = count($data);
$row++;

if ($data[2]==="TRUE")
	{
	echo "user: $data[0] data execution is ON<br />\n";
	echo "$data[0] chance to execute script is $data[3] <br />\n";

	$t=rand(0, 100);

	echo "execution random generator is $t<br />\n";

if ($t<$data[3])
{
echo “Will execute after a delay of $data[4] seconds<br / \n”;
sleep($data[4]);
}
}
else
{
echo “
”;
echo “Code will not be executed”;
echo “
”;
}

echo "<br />";
}

fclose($handle);

}
?>
[/php]

This is a common issue.

echo is just a “Buffer” of information that gets displayed all at once…

So the behavior you describe is normal.

To have the effect you want, you’ll need to flush the buffer to the screen or create your own sleep function that just does a loop, until the required time has elapse.

Thanks for the quick answer. I understand what you’re saying and can easily enough write a time checking look that has the same effect.

that said, I tried loading the page in Chrome (as opposed to Firefox) and it worked. hmmm…

I actually wrote a small test, before I replied to you and I use chrome.

I exhibited the same behavior as you described.

Is there some code I could write - a while loop I am guessing - that gets the current time and stores it in a variable. then takes the current time, adds a variable number of seconds to it and stores THAT value in a new variable then does a while loop until the current time is > the stored time.

so – and I’m just scratching this out so I’m sure syntax is all wrong.

[php]$secondsToWait=19
$currentTime=time();
$timeToExecute=($currenTime+$secondsToWait);

While (Time()<$timeToExecute)
{
echo "I’m waiting…
";
}

echo “its go time!!
”;[/php]

Yeah the time loop is not going to work, the same issue is going to happen unless you flush the output to the browser…

http://php.net/manual/en/function.flush.php

You’ll need to set the buffer size or pad the output before you flush, some browser will only flush the browser after a certain size is reached.

Grrrr. Well, thank you very much for walking me through this, looks like its time to learn about buffer flushing :slight_smile: Your time is most appreciated!

Sorry one more question. Does this buffer/sleep issue happen ONLY with echo? If I don’t need something printed to the screen but, rather, some piece of code executed, will sleep have the desired effect and simply delay the execution of that thing by X seconds or will - once again - all sleep calls within the php file be executed and then the other commands executed.

Yeah, if all you want to do is delay a piece of code and not display anything sleep will work fine.

Thanks, Top!

Sponsor our Newsletter | Privacy Policy | Terms of Service