37

In PHP, I want to put a number of second delay on each iteration of the loop.

for ($i=0; $i <= 10; $i++) {
    $file_exists=file_exists($location.$filename);
    if($file_exists) {
        break;
    }

    //sleep for 3 seconds
}

How can I do this?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Yeak
  • 2,470
  • 9
  • 45
  • 71
  • 1
    Just curious ... Why do you want to delay the loop ? – Baba Mar 14 '13 at 16:25
  • 2
    I should probably point out that a loop that runs 10 times with a 3 second delay will take 30 seconds to execute. This might get your server to timeout, so check your configs and see if you can get some processing done outside of PHP, i.e. via cron or something. – Husman Mar 14 '13 at 16:26
  • 4
    @Husman: 11 times, not 10 times. ;) – Marcel Korpel Mar 14 '13 at 16:27
  • You are indeed correct, pedantism aside, what I said still holds true - Web servers do not like slow scripts and will complain a lot. And a few users on your site, running 33 second scripts on the server is a big NO NO. – Husman Mar 14 '13 at 16:30
  • 1
    @Baba He/She's probably waiting for some other task (like file rights, unzipping, downloading etc) to finish – Sliq Mar 14 '13 at 16:30
  • @Husman I think there are a lot of good scenarious where long-running, low-ressources using scripts or even endless PHP scripts (!) make sense. It's not what PHP was build for, but c'mon, JavaScript was also not build for handling 10.000+ users with one thread on the SERVER - but now it's reality. – Sliq Mar 14 '13 at 16:33
  • @Panique .. `sleep` would just make it worse since `he/she` is not using `threads` ??? Even if its an external process there is no grantee you would get response before 3 sec .. So whats the delay about – Baba Mar 14 '13 at 16:37
  • @Panique - a shotgun can just as well take out your foot if pointed at your foot. Thats no reason to point to it at your foot. There are better solutions than this and I just thought I would point them out. – Husman Mar 14 '13 at 16:39
  • Im just doing the delay to make sure the file is written before processing. What else would u recommend? – Yeak Mar 14 '13 at 16:45
  • is this a page a user can see? After a file upload? – Husman Mar 14 '13 at 16:58
  • You don't need any delay to check if a file has been processed .. What king of processing are you taking about .. external process via `exec` or in PHP – Baba Mar 14 '13 at 17:42
  • Hi Guys Sleep is not working for me. When I use the same my page is not getting loaded. – Puneeth Feb 15 '16 at 09:37
  • @Husman I don't believe sleep() contributes to PHP runtime limits (A script that sleeps for 10 seconds and then runs for 1 second is considered to have taken 1 second of runtime, not 11). Of course I expect the HTTP server may also apply a timeout as well. – GordonM Feb 01 '17 at 09:33

5 Answers5

65

Use PHP sleep() function. http://php.net/manual/en/function.sleep.php This stops execution of next loop for the given number of seconds. So something like this

for ($i=0; $i <= 10; $i++) {
    $file_exists=file_exists($location.$filename);
    if($file_exists) {
        break;
    }
    sleep(3); // this should halt for 3 seconds for every loop
}
mavili
  • 3,385
  • 4
  • 30
  • 46
  • for some reason, this just halts the script for 33 seconds then runs the script with no delay... – Mr PizzaGuy Jan 13 '20 at 18:47
  • @MrPizzaGuy Same Problem with me, did you find the solution? – Arshi Jan 30 '20 at 07:34
  • @MrPizzaGuy this doesn't halt the whole script for the whole of the loop. Try printing something out inside the loop and you will see that script is actually running, but keeps looking for the file if not found then continues to the rest of the script. – mavili Feb 04 '20 at 09:31
15

I see what you are doing... your delaying a script to constantly check for a file on the filesystem (one that is being uploaded or being written by another script I assume). This is a BAD way to do it.

  1. Your script will run slowly. Choking the server if several users are running that script.
  2. Your server may timeout for some users.
  3. HDD access is a costly resource.
  4. There are better ways to do this.

You could use Ajax. And use a timeout to call your PHP script every few seconds. This will avoid the slow script loading. And also you can keep doing it constantly (the current for loop will only run for 33 seconds and then stop).

You can use a database. In some cases database access is faster than HDD access. Especially with views and caching. The script creating the file/uploading the file can set a flag in a table (i.e. file_exists) and then you can have a script that checks that field in your database.

Husman
  • 6,819
  • 9
  • 29
  • 47
  • Would it be better to not write a delay function at all and just put it to run longer maybe 20 times? – Yeak Mar 14 '13 at 16:47
  • 1
    @Yevo No. A loop run 20 times will execute in less than a second. It really depends on the CPU speed. I delay will run in user time (seconds). – Husman Mar 14 '13 at 16:50
  • The thing about depending on AJAX for something like this is that it's easily circumvented. Anything that runs in the client can't be relied on because I can just turn it off or even use a tool like curl to send http commands directly – GordonM Feb 01 '17 at 09:36
7

You can use sleep(3) which sleeps the thread for 3 seconds.

Correction sleep method in php are in seconds.

Philip E
  • 838
  • 9
  • 15
devBinnooh
  • 611
  • 3
  • 12
2

Hare are two ways to sleep php script for some period of time. When you have your code and want to pause script working for some time use these functions. In these examples the first part of code will be done on script run and the second part of code will be done but with time delay.

  1. Using sleep() function you can define sleep time in seconds.

Example:

echo "Message 1";
// The first part of code.
$timeInSeconds = 3;
sleep($timeInSeconds);
// The second part of code.
echo "Message 2";

This way it is possible to sleep php script for 3 seconds. Using this function you can sleep script for whole number (integer) of seconds.

  1. Using usleep() function you can define sleep time in microseconds. This sleep time is convenient for intervals that require more precise time than one second.

Example:

echo "Message 1";
// The first part of code.
$timeInMicroSeconds = 2487147;
usleep($timeInMicroSeconds);
// The second part of code.
echo "Message 2";

You can use this function if you want to sleep php for smaller time values than second (float). In this example I have put script to sleep for 2.487147 seconds.

Nole
  • 796
  • 7
  • 11
0

Have you considered using a PHP Daemon script using supervisorD. I use it in multiple tasks that are required to be running all the time.

The catch is making sure that each time you are running your script you check for memory resources. If its too high, stop the process and then let it restart itself up again.

I have successfully used this process to be always checking database records for tasks to process.

It might be overkill but worth considering.