Current Location: Home> Latest Articles> How to use time_nanosleep to implement batch processing operations with progress bars?

How to use time_nanosleep to implement batch processing operations with progress bars?

gitbox 2025-05-29

When performing batch operations in PHP, you usually encounter the need to show progress to the user during the processing process. Use the time_nanosleep function to achieve high-precision sleep control, thereby simulating the update effect of the progress bar during batch processing. This article will introduce how to use time_nanosleep combined with a simple command line progress bar to implement batch processing operations with progress prompts.


1. Introduction to time_nanosleep

time_nanosleep is a function used in PHP to pause program execution. It allows the program to pause the time at the second and nanosecond levels, in the format as follows:

 bool time_nanosleep(int $seconds, int $nanoseconds);
  • $seconds : integer seconds

  • $nanoseconds : nanoseconds, 1 second = 1 billion nanoseconds

Compared with sleep and usleep , time_nanosleep provides higher precision time control, suitable for scenarios where fine-grained waiting is required.

2. Ideas for implementing progress bars

  1. Set the total number of processing steps, such as 100.

  2. After each part of the task is processed, the update progress bar display is displayed.

  3. The processing time of each step is simulated through time_nanosleep for easy demonstration.

3. Sample code

The following code shows how to use time_nanosleep to implement a command line progress bar.

 <?php
$totalSteps = 50; // Total number of steps
$barWidth = 40;   // Progress bar width

echo "Start a batch operation...\n";

for ($i = 1; $i <= $totalSteps; $i++) {
    // Simulate task processing time,Sleep50millisecond
    time_nanosleep(0, 50 * 1000000); 

    // Calculate progress percentage
    $progress = $i / $totalSteps;
    $filledLength = round($barWidth * $progress);

    // Construct progress bar string
    $bar = str_repeat("=", $filledLength) . str_repeat(" ", $barWidth - $filledLength);

    // Output progress bar and percentage,use "\r" Back to the beginning of the line,Implement dynamic updates
    printf("\r[%s] %.2f%%", $bar, $progress * 100);
    flush();
}

echo "\nBatch operation completed!\n";

Code parsing

  • Use time_nanosleep(0, 50 * 1000000) to pause the program for 50 milliseconds, simulating the processing time per step.

  • Use \r to control characters to return to the beginning of the line to achieve dynamic refresh of the progress bar.

  • printf outputs formatted progress bars and percentages.

  • flush() ensures that the output is displayed in time.

4. Expand thinking

  • If the batch task is a real operation, sleep can be replaced with the specific task execution logic.

  • Combined with output logs or error messages, it is more conducive to debugging and monitoring.

  • $barWidth and sleep time can be adjusted to control the display effect and refresh speed of the progress bar.

5. Reference resources

More official documentation about time_nanosleep can be seen:

 https://gitbox.net/manual/en/function.time-nanosleep.php