time_nanosleep() is a function in PHP to implement nanosecond-level delays. It provides a more accurate pause execution method than sleep() or usleep() . This function may perform differently in different operating environments (such as CLI and the Web), and developers need to pay attention to some key details when using it.
<?php
// pause 2 Second 100 万纳Second(Right now 0.001 Second)
time_nanosleep(2, 1000000);
?>
This function receives two parameters: the first is the number of seconds ( int $seconds ), and the second is the number of nanoseconds ( int $nanoseconds , 0~9999999999). During execution, if there are no errors or interrupts, it will pause the specified time accurately.
CLI environment: When running PHP scripts on the command line, time_nanosleep() performs the closest to the expected pause time. This is because the CLI environment has relatively sufficient resources, less interference, and the operating system scheduling has less impact on latency.
Web environment (such as running through Apache or Nginx): time_nanosleep() may not achieve accurate delay due to Web Server limitations, such as maximum execution time ( max_execution_time ), resource scheduling, concurrent processing, etc. Especially when there are high concurrency or high server load, time control may be interrupted or ignored.
PHP is usually subject to max_execution_time restrictions in web environments, for example:
ini_set('max_execution_time', 3); // The script can be run at most3Second
time_nanosleep(2, 800000000); // Actually waiting to approach2.8Second
If the maximum execution time is exceeded, the script will abort the execution and throw an error. In CLI environments, this limitation usually does not exist and scripts can run for longer.
Using time_nanosleep() in a web environment is usually not a good idea, especially directly used to delay page loading. For example:
<?php
// Not recommended in Web Use directly on the page
time_nanosleep(3, 0);
echo "Page delay loading";
?>
This practice will cause slow page response and poor user experience. The correct approach should be to put delayed operations in asynchronous tasks, queue processing, or front-end loading animations, rather than blocking PHP scripts.
Used to limit the speed when writing crawler and batch tasks in the CLI environment:
foreach ($urls as $url) {
file_get_contents("https://gitbox.net/api/fetch?url=" . urlencode($url));
time_nanosleep(0, 500000000); // pause0.5Second
}
Timely polling scripts, such as listening for file changes:
while (true) {
clearstatcache();
$lastModified = filemtime("/path/to/file.txt");
// Processing logic...
time_nanosleep(1, 0); // 每Second检测一次
}
Nanosecond accuracy is not mandatory : In some operating systems or PHP versions, the accuracy of time_nanosleep() may be insufficient, especially when the incoming nanosecond value is too small.
Interrupt information will be returned when interrupted : If interrupted, it will return an array containing the unfinished sleep time.
Error handling suggestions :
if (time_nanosleep(1, 500000000) === false) {
echo "Delay failure。\n";
}
time_nanosleep() is a powerful but easily misused function. In the CLI environment, it can help developers accurately control the frequency of task execution or implement non-blocking lightweight waiting. In a web environment, you need to be extremely cautious when using this function to avoid affecting the page response speed and user experience. Only by using this function reasonably and cooperating with environmental characteristics and resource limitations can it truly exert its value.