Implementation of php scheduled tasks

Source: Internet
Author: User
Tags php write

I have analyzed in uchome that uchome does this.
1. Store all scheduled tasks in the database
2. Each time a user performs an operation or opens a page, a scheduled task is executed in sequence. That is, when the time is reached, the scheduled task is executed in the user process.
The uchome scheduler task code is in./source/function_cron.php
The above only analyzes the uchome code's rough results about the scheduled task. I hope anyone can share the results with me.
Ignore_user_abort ()
I have thoroughly studied scheduled tasks some time ago. I think there are many ideas to plan tasks, but the most suitable Web is the trigger, which is similar to DZ and PHPWind, it seems that most web applications do the same thing. Besides, there are several other types of web applications for reference.
1. To provide stable triggering, use crontab + wget or AB to provide regular access
2. nohup + php write daemon
3. Writing an endless loop directly using php also provides triggering. In this case, you need to use the cache or database to assist in launching the loop. You must also use sleep or usleep to control the triggering frequency.
4. crontab + php
5. There are also differences between web triggering methods. One is timed triggering, the other is periodic triggering, And the DZ and PHPWind are both timed triggering. I finally use periodic triggering.
The specific implementation idea is easy to think about.
If you have a host, you must use crontab to execute the command line PHP.
No, it can only be written in the code.
It would be better to use crontab. If you use a loop, it is better to separate the loop from the specific processing program, otherwise the memory will keep increasing.

Today, I accidentally found this function in the php manual-ignore_user_abort. This function can help us implement scheduled tasks like cron in linux. Let's take a look at how to implement it.
First, let's take a look at the php Manual's explanation of this function.
Description

Int ignore_user_abort ([bool $ setting])
Sets whether a client disconnect shocould cause a script to be aborted.

That is to say, the following program will be executed no matter whether the client closes the browser.
Let's look at its parameters.
Parameters

Setting
If not set, the function will only return the current setting.

This function accepts a parameter to determine whether to enable the ignore_user_abort function.
Let's look at the returned values:

Return Values
Returns the previous setting, as a boolean.

Here, the previous setting is returned and bool is worth it. After my test, this statement is incorrect. The returned result is of the int type, if you don't believe it, you can write a PHP file to test it.
After talking about this, how should we use the php function to implement the scheduled task? This function is set_time_limit. You can use set_time_limit0 to set the program running time to no limit. The default php running time is 30 seconds, and set_time_limit (0) the program can be executed without limit. Before executing the program, add ignore_user_abort (1) and set_time_limit (0). How can I write the final program? Here is an example.

Copy codeThe Code is as follows: <? Php
Ignore_user_abort (); // run script in background
Set_time_limit (0); // run script forever
$ Interval = 30; // do every 15 minutes...
Do {
$ Fp = fopen('text3.txt ', 'A ');
Fwrite ($ fp, 'test ');
Fclose ($ fp );
Sleep ($ interval); // wait 15 minutes
} While (true );
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.