PHP.mk документација

time_nanosleep

Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.

function.time-nanosleep.php PHP.net прокси Преводот е вчитан
Оригинал на PHP.net
Патека function.time-nanosleep.php Локална патека за оваа страница.
Извор php.net/manual/en Оригиналниот HTML се реупотребува и локално се стилизира.
Режим Прокси + преведен приказ Кодовите, табелите и белешките остануваат читливи во истиот тек.
time_nanosleep

Референца за `function.time-nanosleep.php` со подобрена типографија и навигација.

function.time-nanosleep.php

time_nanosleep

класата mysqli_driver

time_nanosleep(PHP 5, PHP 7, PHP 8)

= NULL

time_nanosleep(int $seconds, int $nanoseconds): array|bool

Одложи за одреден број секунди и наносекунди seconds and nanoseconds.

Параметри

seconds

Го одложува извршувањето на програмата за дадениот број на

nanoseconds

Мора да биде не-негативен цел број.

Забелешка: Мора да биде не-негативен цел број помал од 1 милијарда.

Вратени вредности

Патеката до PHP скриптата што треба да се провери. true на успех или false при неуспех.

На Windows, системот може да спие подолго од дадениот број на наносекунди, во зависност од хардверот.

  • seconds Ако одложувањето беше прекинато со сигнал, ќе се врати асоцијативен список со компонентите: - број на преостанати секунди во одложувањето - број на преостанати наносекунди во одложувањето
  • nanoseconds anybody (a) emuxperts.net

Примери

Пример #1 time_nanosleep() example

<?php
// Careful! This won't work as expected if an array is returned
if (time_nanosleep(0, 500000000)) {
echo
"Slept for half a second.\n";
}

// This is better:
if (time_nanosleep(0, 500000000) === true) {
echo
"Slept for half a second.\n";
}

// And this is the best:
$nano = time_nanosleep(2, 100000);

if (
$nano === true) {
echo
"Slept for 2 seconds, 100 microseconds.\n";
} elseif (
$nano === false) {
echo
"Sleeping failed.\n";
} elseif (
is_array($nano)) {
$seconds = $nano['seconds'];
$nanoseconds = $nano['nanoseconds'];
echo
"Interrupted by a signal.\n";
echo
"Time remaining: $seconds seconds, $nanoseconds nanoseconds.";
}
?>

Види Исто така

  • sleep() - Одложи извршување
  • usleep() - Задржи извршување во микросекунди
  • time_sleep_until() - Натерај го скриптот да спие до одредено време
  • set_time_limit() - Направете скриптата да спие до одредено време

Белешки од корисници 4 белешки

m at kufi dot net
19 години пред
Documentation states that "seconds" must be positive. This is not correct, 0 is possible.

Rather, "seconds" must be non-negative.
fantasysportswire at yahoo dot com
20 години пред
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.";
 ?>
b dot andrew at shaw dot ca
19 години пред
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......

<?php

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 :( .....
Одложи за одреден број секунди и наносекунди
пред 17 години
A response to the note below:

Your function is also useless, as the WinNT 32 kernel only functions at a minimum of about 10+ ms (1,000 us), rendering usleep() useless, because usleep uses the C function which is provided by the system (in this case, kernel32.dll).

You'll want to use a function that does not rely on the kernel, but rather something made for precise measurement:

<?php
function usleep_win( $micro_seconds )
{
    if ( @function_exists( "socket_create" ) && @function_exists( "socket_select" ) )
    {
        $false = NULL;
        $socket = array( socket_create( AF_INET, SOCK_RAW, $false ) );
        socket_select( $false, $false, $socket, 0, $micro_seconds );
        return true;
    }
    else
    {
        return false;
    }
}
?>

This function will allow to you sleep for a specified microsecond, although I have measured it to be off by ~5 us.

Again, most of this depends on the hardware in your system. If you _REALLY_ need to be precise to < 10 us, you shouldn't be using WinNT anyways!
На оваа страница

Автоматски outline од активната документација.

Насловите ќе се појават тука по вчитување.

Попрегледно читање

Примерите, changelog табелите и user notes се визуелно издвоени за да не се губат во долгата содржина.

Брз совет Користи го outline-от Скокни директно на главните секции од активната страница.
Извор Оригиналниот линк останува достапен Кога ти треба целосен upstream context, отвори го PHP.net во нов tab.