Just glancing at this - and the note from over a year ago with a implementation for windows.. with 5.0.0 and higher it would be simplier to just do something like......
<?
if (!function_exists('time_nanosleep')) {
function time_nanosleep($seconds, $nanoseconds) {
sleep($seconds);
usleep(round($nanoseconds/100));
return true;
}
}
?>
....off the top of my head - obviously simple enough there should be no mistakes.. but those are the ones that always seem to get ya :( .....
time_nanosleep
(PHP 5)
time_nanosleep — Retardo por un número de segundos y nanosegundos
Descripción
Retarda la ejecución del programa por el número dado de segundos y nanosegundos .
Lista de parámetros
- segundos
-
Debe sen un entero positivo.
- nanosegundos
-
Debe ser un entero positivo menor a mil millones.
Valores retornados
Devuelve TRUE si todo se llevó a cabo correctamente, FALSE en caso de fallo.
Si el retardo fue interrumpido por una señal, se devolverá una matriz asociativa con los componentes:
- seconds - número de segundos restantes en el retardo
- nanoseconds - número de nanosegundos restantes en el retardo
Ejemplos
Example #1 Ejemplo de time_nanosleep()
<?php
// ¡Cuidado! Esto no funcionará como se espera si se devuelve una matriz
if (time_nanosleep(0, 500000000)) {
echo "Durmió por medio segundo.\n";
}
// Esto es mejor:
if (time_nanosleep(0, 500000000) === true) {
echo "Durmió por medio segundo.\n";
}
// Y este es el mejor método:
$nano = time_nanosleep(2, 100000);
if ($nano === true) {
echo "Durmió por 2 segundos, 100 milisegundos.\n";
} elseif ($nano === false) {
echo "El retardo fallo.\n";
} elseif (is_array($nano)) {
$segundos = $nano['seconds'];
$nanosegundos = $nano['nanoseconds'];
echo "Interrumpido por una señal.\n";
echo "Tiempo restante: $segundos segundos, $nanosegundos nanosegundos.";
}
?>
Notes
Note: Esta función no está implementada en plataformas Windows.
Ver también
time_nanosleep
fantasysportswire at yahoo dot com
19-Dec-2006 12:27
19-Dec-2006 12:27
anybody (a) emuxperts.net
22-Aug-2006 01:03
22-Aug-2006 01:03
Documentation states that "seconds" must be positive. This is not correct, 0 is possible.
Rather, "seconds" must be non-negative.
m at kufi dot net
13-Aug-2005 11:03
13-Aug-2005 11:03
You should take into account, if you use the function replacement down here, the CPU will be in use of 99% for the time of execution...
(A little bit better in this situation is to let the 'full seconds' go by a normal sleep command (makes the thread sleep!, and uses minimum cpu))
<?php
//THIS IS THE FUNCTION WE ARE TALKIN ABOUT
function timeWait($microtime)
{
//optimizations added by me [start]
//sleep the full seconds
sleep(intval($microtime));
//set the microtime to only resleep the last part of the nanos
$microtime = $microtime - intval($microtime);
//optimizations added by me [end]
$timeLimit = $microtime + array_sum(explode(" ",microtime()));
while(array_sum(explode(" ",microtime())) < $timeLimit)
{/*DO NOTHING*/}
return(true);
}
//THIS IS HOW WE CAN USE IT
echo "Process started at " . date("H:i:s") . " and " . current(explode(" ",microtime())) . " nanoseconds.<br>";
timeWait(5.5); //With this call the system will wait 5 seconds and a half. You can use either integer or float.
echo "Process completed at " . date("H:i:s") . " and " . current(explode(" ",microtime())) . " nanoseconds.";
?>
tecnomaniac at ig dot com dot br
27-Jul-2005 12:04
27-Jul-2005 12:04
This is an alternative function to sleep_nanosecond that you can use with PHP versions below PHP 5.0. It is not very accurate if we talk about nanoseconds but the results are satisfatory. Enjoy!
<?php
//THIS IS THE FUNCTION WE ARE TALKIN ABOUT
function timeWait($microtime)
{
$timeLimit = $microtime + array_sum(explode(" ",microtime()));
while(array_sum(explode(" ",microtime())) < $timeLimit)
{/*DO NOTHING*/}
return(true);
}
//THIS IS HOW WE CAN USE IT
echo "Process started at " . date("H:i:s") . " and " . current(explode(" ",microtime())) . " nanoseconds.<br>";
timeWait(5.5); //With this call the system will wait 5 seconds and a half. You can use either integer or float.
echo "Process completed at " . date("H:i:s") . " and " . current(explode(" ",microtime())) . " nanoseconds.";
?>
