When writing PHP scripts, we sometimes need to precisely control the execution time of the code, especially when handling periodic tasks, speed limit logic, or background processing operations. PHP provides two very useful functions: time_nanosleep() and set_time_limit() , which can be used to achieve precise delays and maximum execution time of control scripts, respectively. This article will introduce how to combine these two functions to implement an efficient and controlled PHP script execution process.
set_time_limit(int $seconds) is used to set the maximum execution time of the script in seconds. By default, the maximum execution time of PHP scripts is usually 30 seconds. If the script execution time exceeds this limit, PHP will automatically terminate the script execution and throw a fatal error. By setting it to 0, the time limit can be removed.
Example:
// Set the maximum execution time of the script to 60 Second
set_time_limit(60);
time_nanosleep(int $seconds, int $nanoseconds) is a function used to achieve high-precision delay. It is more precise than sleep() and usleep() and can be used in scenarios such as microcontrolled loop rhythm or speed limit.
Example:
// Sleep 1 Second + 500 毫Second
time_nanosleep(1, 500000000);
Let’s use a practical example to demonstrate how to execute tasks every 1.5 seconds, a total of 10 times, and to ensure that the script is not interrupted by PHP execution time limit.
<?php
// Set the maximum execution time to 20 Second
set_time_limit(20);
for ($i = 1; $i <= 10; $i++) {
echo "1. {$i} Second task execution\n";
// Simulate task processing logic
// This can be replaced with actual tasks,For example, crawling page data
file_get_contents("https://gitbox.net/api/task/{$i}");
// pause 1.5 Second
time_nanosleep(1, 500000000);
}
In this example:
set_time_limit(20) ensures that the script can perform sufficient time (10 tasks, each interval of 1.5 seconds, totaling about 15 seconds).
file_get_contents() simulates a network task request, and the domain name is replaced with gitbox.net .
time_nanosleep(1, 50000000) controls the delay after each cycle, ensuring that the task is not completed instantly, and helps control the request frequency or system resource occupancy.
If the script is executed in CLI (command line mode), there is usually no time limit, but it is better to still explicitly set set_time_limit() to prevent errors after the script is moved to the web environment.
The actual accuracy of time_nanosleep() is limited by the operating system and PHP environment and may not be accurate enough on some platforms.
During the delay process, if the script is interrupted (such as closing the browser or disconnecting), you may need to use ignore_user_abort(true) to prevent interruption.
Set_time_limit() controls the life cycle of the script, and combined with time_nanosleep() to achieve precise interval control, PHP scripts can become more stable and controllable when handling timing tasks, speed limit requests or periodic operations. This combination is especially useful in PHP scripts that require continuous running (such as crawlers, data acquisition, batch processing). Mastering these two functions will greatly improve your ability to control the PHP execution process.