PHP sleep delay
Asked Answered
L

5

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?

Langer answered 14/3, 2013 at 16:16 Comment(13)
Just curious ... Why do you want to delay the loop ?Jalopy
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.Obreption
@Husman: 11 times, not 10 times. ;)Riel
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.Obreption
@Jalopy He/She's probably waiting for some other task (like file rights, unzipping, downloading etc) to finishRumelia
@Obreption 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.Rumelia
@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 aboutJalopy
@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.Obreption
Im just doing the delay to make sure the file is written before processing. What else would u recommend?Langer
is this a page a user can see? After a file upload?Obreption
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 PHPJalopy
Hi Guys Sleep is not working for me. When I use the same my page is not getting loaded.Meloniemelony
@Obreption 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.Petrous
C
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
}
Control answered 14/3, 2013 at 16:18 Comment(3)
for some reason, this just halts the script for 33 seconds then runs the script with no delay...Sezen
@MrPizzaGuy Same Problem with me, did you find the solution?Zorina
@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.Control
O
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.

Obreption answered 14/3, 2013 at 16:39 Comment(3)
Would it be better to not write a delay function at all and just put it to run longer maybe 20 times?Langer
@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).Obreption
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 directlyPetrous
W
7

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

Correction sleep method in php are in seconds.

Wunderlich answered 14/3, 2013 at 16:20 Comment(4)
do confuse sleep with usleep()Phyllode
it's sleep(3), not sleep(3000).Rumelia
sleep(3000) will sleep for nearly an hour!Cuthbertson
lol, yes, thank you guys I just fixed it. I got mixed up with java's sleep method.Wunderlich
H
3

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.

Haematoblast answered 31/1, 2017 at 15:59 Comment(0)
G
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.

Guava answered 31/5, 2016 at 2:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.