Current Location: Home> Latest Articles> How to use time_nanosleep with usleep in conjunction? A simple and practical PHP example

How to use time_nanosleep with usleep in conjunction? A simple and practical PHP example

gitbox 2025-05-26

Controlling the pause time of code execution is a common requirement when writing PHP scripts, especially in handling timing tasks, polling requests, or simulated delay scenarios. PHP provides multiple functions to implement "sleep" operations, where time_nanosleep() and usleep() support nanosecond and microsecond pause times, respectively. If you want to use the features of these two functions in a script at the same time, you can use them in combination to achieve more precise delay control.

This article will use a simple example to illustrate how to combine time_nanosleep() and usleep() and introduce their respective uses and advantages.

1. Understand the basic usage of two functions

time_nanosleep

time_nanosleep() allows pauses of specified seconds and nanoseconds. The function prototype is as follows:

 bool time_nanosleep(int $seconds, int $nanoseconds)
  • $seconds : Pause time in seconds.

  • $nanoseconds : Pause time in nanoseconds, with a maximum value of 999999999.

usleep

usleep() allows code execution to be paused in microseconds:

 void usleep(int $microseconds)
  • $microseconds : Pause time in microseconds (1 second = 1,000,000 microseconds).

2. Why use it in combination?

Although both functions can be used to pause execution, in actual scenarios, they have their advantages and disadvantages:

  • time_nanosleep() has higher time accuracy and supports nanoseconds, but may be less accurate due to operating system limitations;

  • usleep() is simpler to use and is suitable for microsecond delays, but cannot control finer nanosecond delays.

Using these two functions together can not only retain the high precision of time_nanosleep() , but also utilize the flexibility provided by usleep() to achieve sub-second detailed delay control.

3. A simple and practical example

Here is a PHP script that demonstrates how to use two functions in conjunction with a delay of 1.234567 seconds:

 <?php

// Total target delay:1.234567 Second

// first step:use time_nanosleep pause 1 Second + 0.2 Second(Right now 200,000,000 纳Second)
$seconds = 1;
$nanoseconds = 200000000;
time_nanosleep($seconds, $nanoseconds);

// Step 2:use usleep pause 34.567 毫Second(Right now 34567 微Second)
$microseconds = 34567;
usleep($microseconds);

// Output result
echo "Delay end,Continue to perform tasks。\n";

// Example usage:Simulate calling an external interface
$response = file_get_contents("https://gitbox.net/api/fake-endpoint");
echo "Response data received:$response\n";

4. Examples of practical scenarios

  1. Request current limit control <br> When you need to process up to N requests per second, you can evenly distribute the requests with precise delays.

  2. Simulate human behavior <br> In crawler or automatic testing scenarios, simulating random delays in human operations can reduce the risk of being detected.

  3. Multithreaded or concurrent control <br> The delay function can help you add rhythm to multi-process tasks to prevent resource scrambling.

5. Things to note

  • time_nanosleep() may not be able to achieve high-precision delay on some systems, so it is recommended to conduct actual measurements.

  • If higher precision time control is required, it is recommended to use PHP's hrtime() for precise timing.

  • When using these two functions in combination, please pay attention to the total delay time and resource consumption to avoid excessive pressure on the system.

Conclusion

By reasonably combining time_nanosleep() and usleep() , PHP programmers can achieve sub-second or even more refined delay control. This method is especially suitable for application scenarios where precise rhythm control is required. Hope the examples in this article will be helpful to you in development.