usleep

(PHP 3, PHP 4, PHP 5)

usleep -- Delay execution in microseconds

Description

void usleep ( int micro_seconds )

The usleep() function delays program execution for the given number of micro_seconds. A microsecond is one millionth of a second.

Пример 1. usleep() example

<?php

// Current time
echo date('h:i:s') . "\n";

// wait for 2 secondes
usleep(2000000);

// back!
echo date('h:i:s') . "\n";

?>

This script will output :

11:13:28
11:13:30

Замечание: This function did not work on Windows systems until PHP 5.0.0

See also sleep() and set_time_limit().



usleep
gizmo at aoaforums dot com
02-Feb-2006 09:28
It should be noted that Windows machines have a resolution of either 10 mS or 15 mS (depending on the chipset implementation and HAL used) when using the Sleep() function in kernel32.dll.  This means that your average error will be either 5 or 7.5 mS.  This is not ordinarily a problem unless you really NEED to sleep for less time than the granularity provided by Windows.
gmc at serveisw3 dot net
07-Jul-2005 09:11
If you're using Windows then you maybe are in trouble with usleep if you really need to use it.

The Bernie's microdelay function using fsockopen does not work properly, and the fclose doesn't help much.

I don't know if network connections go strange, but I know it does not work since you've made more than 2000 - 3000 calls to it, so it's not a reliable solution in 'long life' php scripts, or these are the issues of the microdelay function in my PHP and PHP-GTK applications.

Though another solution should be found, and googling a bit I fount a WinAPI function: Sleep.

So I get with this snippet wich works fine for me, you get milliseconds precission but the more important, it works for long-run scripts and of course, it does not waste any CPU cycles.

dl('php_w32api.dll');

$GLOBALS['win32api'] =& new win32;

// USleep alternative for Windows and PHP4:
$GLOBALS['win32api']->registerfunction("long Sleep (long dwMillisecods) From kernel32.dll");

// Now you can call the function from everywhere in your script: $GLOBALS['win32api']->Sleep(milliseconds);

for ($msec = 2000; $msec > 0; $msec = $msec - 125) {
  echo "Hi. Next one in $msec msec.\n";
  $GLOBALS['win32api']->Sleep($msec);
}
t0russ at gmail dot com
03-May-2005 08:06
solution to the warning posted by Bertie:
$f=@fsockopen("tcp://localhost",$UNUSED_PORT,$errno,$errstr,$delay);
@fclose($f);
brian dot hollenbeck at gmail dot com
25-Mar-2005 04:28
Just a tip for the folks at home: is you use multiple rand() functions in your code, be sure to put a randomized usleep() in between the rand() codelines, or else your "random" seeds won't be so random on a fast server.

Example:

$string = rand(0, 100);
$string = rand(0, 200);

Instead:

$string = rand(0, 100);
usleep(rand(1000, 10000));
$string1 = rand(0, 100);
Bertie
28-Nov-2003 11:47
A word of warning about the microdelay() code posted that uses the fsockopen - if you use this is a loop that delays for small periods you will very quickly run out of sockets/socket buffer space. And then your network connections go very strange......
grail dot ink at unix dot net
29-Aug-2003 02:06
We have developed an alternative to usleep that should work on any platform and it doesn't eat up any CPU cycles. It's been tested on Windows 2000 w/ Apache. Other platforms should work fine.

function microdelay($delay) {
$UNUSED_PORT=31238; //make sure this port isn't being used on your server
@fsockopen("tcp://localhost",$UNUSED_PORT,$errno,$errstr,$delay);
}

It times out after a specified time in seconds. It uses a double as argument so you can specify decimal arguments.

microdelay(.5); //500ms
microdelay(.25); //250ms
microdelay(2.25); //2250ms

Our small contribution to this great language!

http://www.xencoders.com
jadd0r at mail dot com
08-Apr-2003 10:59
You could do neat things with this like for unix:

<?php
$y
=1000000;
for (
$x=1; $x<50; $x++) {
echo
chr(7);
usleep($y*(pow(0.9,$x)+0.2));
}
?>

then execute
`php -q script.php > /dev/console`

will result in a bombtimer (chr(7) is beep character) :]
busby at edoceo dot com
17-Jan-2003 04:04
Should be noted that functions that loop really fast to create a delay also consume 100% CPU while doing the loop.  Try creating a dummy loop that goes 100000 times, watch it choke your machine.  If you really need usleep() don't use windows.
dsc at c2i dot net
14-Mar-2002 09:55
The usleep() for Windows above doesn't take into account that the value of $stop can be lower than $start. It also contains unnecessary casts and $temp variable. Here is a better function:

function usleepWindows($usec)
{
   $start = gettimeofday();

   do
   {
       $stop = gettimeofday();
       $timePassed = 1000000 * ($stop['sec'] - $start['sec'])
           + $stop['usec'] - $start['usec'];
   }
   while ($timePassed < $usec);
}
dave at techweavers dot net
28-Dec-2000 07:29
To monitor a scripts CPU ussage and avoid any nasty CPU gobbling loops you can use this function (will not work with windows or safe mode) I know it works on FreeBSD:
function phpmon($max)
 {
 $cmd = `ps -Unobody -r -o%cpu`;
 $lines = explode("\n", $cmd);
 $usage = substr($lines[1], 0, strpos($lines[1], "."));
 $sleeprate = 500;
 while ($usage >= $max)
  {
  $cmd = `ps -Unobody -r -o%cpu`;
  $lines = explode("\n", $cmd);
  $usage = substr($lines[1], 0, strpos($lines[1], "."));
  usleep($sleeprate);
  }
 }

phpmon($MAX);

where $MAX is the maximum CPU you want the process to consume. e-mail me with any improvements/suggestions.

I have noticed that this consumes a lot of system CPU (at least in my limited testing) possibly from all of the system calls or the huge mathematical functions I used to test the effectiveness of the script.

<unpackmnoGoSearch>
 Last updated: Tue, 15 Nov 2005