|
Take a look at the settings for the scheduled tasks in Uchome: 1, to put all the scheduled tasks into Database 2, each time the user to operate or open the page by a sort of a scheduled task. That is, take turns to judge, the time is up, in the user process execution. Uchome's scheduled task code on the./source/function_cron.php only analyzes the Uchome code for the approximate outcome of the scheduled task.
Ignore_user_abort () I have thoroughly studied the planning task in the previous period, think that the planning task of a lot of ideas, but the most suitable for the Web or trigger-like, is similar to DZ and phpwind, it seems that most Web applications are doing so, and several other for reference 1, In order to provide a stable trigger, with crontab+wget or AB to provide timed access to 2,nohup + PHP write daemon 3, directly in PHP to write a dead loop also provides a trigger, then need to use a cache or database to help roll out the loop, There must be sleep or usleep to control the trigger frequency 4,crontab+php5,web trigger mode, which also has a difference, one is the timing trigger, a fixed period trigger, DZ and phpwind are timing trigger, I finally use the fixed cycle trigger
Own host must use Crontab to execute command line PHP is not only written in the code in the crontab will be better, if the use of loops, it is best to separate the cycle and specific processing procedures, otherwise the memory will always increase.
The PHP manual found this function-ignore_user_abort, this function can help us to implement a cron like Linux in the implementation of the planning task, the following to see how to implement. First, take a look at the PHP manual's interpretation of this function Descriptionint ignore_user_abort ([bool $setting]) sets whether a client disconnect should cause a SCR IPT to be aborted. That is, the following program executes regardless of whether the client closes the browser.
Then take a look at its parameters: Parameterssettingif not set, the function would only return the current setting. It takes a parameter to decide whether to enable Ignore_user_ The Abort function.
Then look at its return value: Return valuesreturns the previous setting, as a Boolean. Here it is said to return the previous setting, and is bool worth, after my test, this statement is not correct, the return is clearly int type.
How to use this function of PHP to achieve the planning task? Also fall with the help of another function, this function is Set_time_limit, through Set_time_limit0) can set the program run time is unlimited, PHP default run time is 30 seconds, through Set_time_ Limit (0) allows the program to execute indefinitely.
It is possible to add ignore_user_abort (1) and Set_time_limit (0) before the execution of the program, and how should the final program be written?
Example:
- Ignore_user_abort (); Run script in background
- Set_time_limit (0); Run Script Forever
- $interval = 30; Do every minutes ...
- do{
- $fp = fopen (' Text3.txt ', ' a ');
- Fwrite ($fp, ' test ');
- Fclose ($FP);
- Sleep ($interval); Wait minutes
- }while (TRUE);
- ?>
Copy Code |