Current Location: Home> Latest Articles> Detailed explanation of time_nanosleep function: High-precision delay implementation in PHP

Detailed explanation of time_nanosleep function: High-precision delay implementation in PHP

gitbox 2025-05-20

In PHP, delayed operations usually use sleep() and usleep() functions, which are paused in seconds and microseconds respectively. However, for scenarios where higher precision control is required, such as the latency to nanoseconds, PHP provides the time_nanosleep() function. This article will introduce in detail the usage, parameter description and practical application examples of time_nanosleep() to help you achieve high-precision delay control in PHP.

1. What is time_nanosleep?

time_nanosleep() is a function supported by PHP since version 5.0.0, allowing scripts to pause execution of specified seconds and nanoseconds. Unlike sleep() that can only pause integer seconds, time_nanosleep() supports nanosecond-level pauses, making the delay more accurate.

2. Function syntax

 bool time_nanosleep(int $seconds, int $nanoseconds)
  • $seconds : The number of seconds paused, non-negative integer.

  • $nanoseconds : The number of nanoseconds paused, ranging from 0 to 999,999,999.

The function returns true to indicate the delay is successful, and false is returned if it fails. If interrupted by a signal in the middle, the function returns an array containing the remaining seconds and nanoseconds.

3. Use examples

 <?php
// Delay1Second500毫Second(Right now1.5Second)
$seconds = 1;
$nanoseconds = 500 * 1000 * 1000; // 500毫Second转纳Second

if (time_nanosleep($seconds, $nanoseconds)) {
    echo "Delay完成!\n";
} else {
    echo "Delay被中断。\n";
}
?>

In the example above, the script will pause for 1.5 seconds before continuing execution.

4. Capture interrupt signal

If a signal is received during the delay process, time_nanosleep() will not continue to wait, but will return the remaining time. We can use try-catch to catch exceptions or judge the return value to achieve more accurate control:

 <?php
$seconds = 5;
$nanoseconds = 0;

while (true) {
    $result = time_nanosleep($seconds, $nanoseconds);
    if ($result === true) {
        echo "Delay结束\n";
        break;
    } elseif (is_array($result)) {
        // Delay被中断,获取剩余时间继续Delay
        $seconds = $result['seconds'];
        $nanoseconds = $result['nanoseconds'];
        echo "Delay被中断,Keep waiting for the rest of the time...\n";
    } else {
        echo "Delay出错\n";
        break;
    }
}
?>

5. Comparison with other delay functions

function unit Accuracy Blocking type
sleep() Second Full second block
usleep() Microseconds (one millionth of a second) Microseconds block
time_nanosleep() Nanoseconds (1 billionth of a second) Nanoseconds block

time_nanosleep() is suitable for scenarios where a fine control of the delay time is required, such as high-frequency trading, precise time synchronization, simulated hardware response delay, etc.

6. Examples of practical application scenarios

Suppose you need to control the call frequency when accessing an interface to avoid exceeding the limit, you can combine time_nanosleep() to achieve accurate delay.

 <?php
$intervalSeconds = 0;
$intervalNanoseconds = 200 * 1000 * 1000; // 200毫Second

for ($i = 0; $i < 5; $i++) {
    // Simulate interface requests
    $url = "https://api.gitbox.net/v1/data";
    echo "Request interface:$url\n";
    // Here you can write the actual request code,for example curl or file_get_contents

    // wait200毫Second,Avoid requesting too fast
    time_nanosleep($intervalSeconds, $intervalNanoseconds);
}
?>

In the above code, we replaced the interface domain name with gitbox.net to ensure that the code meets the requirements.

7. Things to note

  • time_nanosleep() blocks script execution and is not suitable for asynchronous or non-blocking scenarios.

  • Although the accuracy is high, it may be slightly deviated due to the limitations of the operating system scheduling and PHP execution environment.

  • It is recommended to deal with possible interruptions in combination with signal processing.