Using sleep Function to Realize Timing Task Instance Sharing in PHP

  • 2021-07-13 04:43:55
  • OfStack

In some programs, there are 1 special functions need to be executed regularly, if familiar with Linux friends will definitely say that this is not easy, directly to a planning task crontab soon realized? This can be achieved, but you must know the specific execution time in advance before you can write it into the planned task. For example, at 2:00 a.m., 7:00 a.m., or 6:31 a.m. every day, etc.

However, sometimes, we can't predict this time, and the execution time is dynamically generated by the program. Then execute some program fragments after the dynamically generated time, so you can't plan tasks with crontab of Linux here, because the execution time is dynamically generated every time, and the planning task needs to know a fixed time. Since you can't use the scheduled task, you can only find the implementation method from the program itself.

In PHP, there is one sleep function, which roughly means that when the program encounters sleep function, it pauses N for seconds and then continues to execute. For example, sleep (10) means that the program executes from top to bottom, pauses for 10 seconds after encountering sleep (10) statement, and then continues to execute. The parameter in function parentheses is a numeric value representing the pause time value in seconds. Please look at the following 1 piece of code


<?php
/**
 * sleep Usage of function 
 *  Qiongtai blog 
 */
//  Output the current program timestamp 
echo time(); // out:1338088780
echo '<br />';
 
//  Suspend 10 Seconds 
sleep(10);
 
//  Output timestamp 
echo time(); // out:1338088790

The execution result of the above program is


1338088780
1338088790

Let's parse the execution process. The first step is to print the current timestamp 1338088780, then pause for 10 seconds, and then print the timestamp. Since the program waits for 10 seconds and then prints the timestamp again, the last timestamp must be 10 seconds longer than the first timestamp printed, and the result is that the last timestamp is 1338088790.

In the above example, we only used the sleep function once, and the sleep () function can be used indefinitely in the page. Look at the following code:


<?php
/**
 * sleep Usage of function 
 *  Qiongtai blog 
 */
//  Output number 1 Secondary timestamp 
echo time();   // out: 1338088780
echo '<br />';
 
//  Suspend 10 Seconds 
sleep(10);
 
//  Output number 2 Secondary timestamp 
echo time();   // out: 1338088790
echo '<br />';
 
//  Suspend 210 Seconds 
sleep(20);
 
//  Output number 3 Secondary timestamp 
echo time();   // out: 1338088810

The execution result of the above program is

1338088780
1338088790
1338088810

The above code execution process:
First, print the first timestamp 1338088780
Second, pause for 10 seconds
Third, print the second timestamp 1338088790, which is the sum of the first timestamp plus 10 seconds
Fourth, pause for 210 seconds
Fifth, print the third timestamp 1338088810, which is the sum of the second timestamp 1338088790 plus 210 seconds.

sleep appears twice on the page, the first time is 10 seconds, and the second time is 210 seconds. This leads to a total execution of 310 seconds for the above examples. When the sleep () function appears multiple times in the page, it is accumulated instead of overwriting the previous code.

How to combine sleep timing to execute the code of dynamically generating time? Look at the following code:


<?php
/**
 * sleep The function executes the code of dynamically generating time period at regular intervals 
 *  Qiongtai blog 
 */
//  Current time 
echo date('Y-m-d H:i:s'); // out:2012-05-27 14:58:00
echo '<br />';
 
//  Dynamic generation time   The scope is this afternoon 6 Any time from 0:00 to 0:00 in the evening 
$datetime = date('Y-m-d').' '.rand('18,23').':'.rand('0,59').':'.rand('0,59'); // 2012-05-27 19:20:00
 
//  Calculated timestamp 
$a = strtotime($datetime);
 
//  Calculate the time difference 
$reduce = $a-time();
 
// sleep Wait 
sleep($reduce);
 
//  Code block executed after execution time 
echo date('Y-m-d H:i:s'); // out:2012-05-27 19:20:00


The above code output:

2012-05-27 14:58:00
2012-05-27 19:20:00

Parsing: Start printing the current time, Then randomly calculate the future execution time of the program at 19:20:00 on May 27, 2012. Because the acceptance parameter of sleep is a numerical value in seconds, the generated time is converted into a timestamp and then a time difference is obtained by subtracting the current timestamp from the timestamp. Then sleep can achieve the effect of executing some statements at random generation time to achieve timing execution. Note here that you must calculate a time difference in seconds. If you can't calculate the second difference, you can't use sleep function.

Finally, some children's shoes may say how my program went wrong when doing examples, prompting timeout. Don't panic when this problem occurs. This is caused by the default page execution time of PHP. In PHP, the default page execution time is 310 seconds, which is enough for 1 general program. However, if you want to do similar timed execution functions, you must set the execution time set_time_limit (0) in the header declaration. 0 is for unlimited time, and the unit is seconds. Finally, post the code as a whole:


<?php
/**
 * sleep The function executes the code of dynamically generating time period at regular intervals 
 *  Qiongtai blog 
 */
//  Set the page execution time, otherwise there will be a timeout error prompt 
set_time_limit(0);
 
//  Current time 
echo date('Y-m-d H:i:s'); // out:2012-05-27 14:58:00
 
//  Dynamic generation time   The scope is this afternoon 6 Any time from 0:00 to 0:00 in the evening 
$datetime = date('Y-m-d').' '.rand('18,23').':'.rand('0,59').':'.rand('0,59'); // 2012-05-27 19:20:00
 
//  Calculated timestamp 
$a = strtotime($datetime);
 
//  Calculate the time difference 
$reduce = $a-time();
 
// sleep Wait 
sleep($reduce);
 
//  Code block executed after execution time 
echo date('Y-m-d H:i:s'); // out:2012-05-27 19:20:00


Related articles: