I was uchome in the analysis that Uchome was doing this.
1. Keep all the scheduled tasks in the database
2. Each time a user operates or opens a page, it performs a scheduled task in sorted order. That is to take turns to judge, time is over, in the user process execution.
Uchome's scheduled task code is in./source/function_cron.php
The above only analyzes the Uchome code about the planning task of the approximate results, a discussion. Hope who has a good way to take out the share.
Ignore_user_abort ()
I had a thorough study of the planning tasks in the previous period, thinking that there are a lot of ideas for planning tasks, but the most suitable for the web or the trigger, is similar to DZ and phpwind, it seems that most Web applications are doing so, and several others for reference
1, in order to provide a stable trigger, with crontab+wget or AB to provide regular access
2,nohup + PHP Write daemon
3, directly using PHP to write a dead loop also provides a trigger, you need to use a cache or database to facilitate the launch cycle, and must use sleep or usleep control trigger frequency
4,crontab+php
5,web trigger mode, which also has a difference, one is a timed trigger, one is a fixed-cycle trigger, DZ and phpwind are timing triggers, I finally adopted a fixed-cycle trigger
The concrete realization thought is very easy to think, does not say more
Own host must use Crontab to execute command line PHP
No, it's just written in the code.
With crontab will be better, if the cycle, it is best to put the cycle and the specific processing program separate, otherwise the memory will continue to increase.
I accidentally found this function-ignore_user_abort in the PHP manual today, this function can help us to implement the planning task like the Cron in Linux, let's look at how to implement it.
First look at the PHP manual to explain this function
Description
int Ignore_user_abort ([bool $setting])
Sets whether a client disconnect should cause a script to be aborted.
This means that the following programs execute regardless of whether the client closes the browser.
And look at the parameters.
Parameters
Setting
If not set, the function would only return to the current setting.
This function takes a parameter to determine whether to enable Ignore_user_abort functionality.
And look at its return value:
Return Values
Returns the previous setting, as a Boolean.
Here said to return to the previous settings, and is bool worth, after my test, this argument is wrong, the return is clearly int type, do not believe that you can write a php file to test.
Said so much, in the end how to use this function of PHP to achieve the planning task? Also fall with another function, this function is Set_time_limit, through set_time_limit0, you can set the running time of the program is unlimited, PHP default run time is 30 seconds, The Set_time_limit (0) allows the program to execute indefinitely. It is OK to add ignore_user_abort (1) and Set_time_limit (0) before the execution of the program, how should the final program be written? give you an example.
The
Code is as follows: <?php 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);//M Inutes}while (true);?>