Use the PHP function ignore_user_abort to implement scheduled tasks and continuous process instances.

Source: Internet
Author: User
The PHP function ignore_user_abort is used to implement the second phase of the PHP built-in function research series, and the PHP function ignore_user_abort is used to implement the scheduled task and the continuous process instance, we also discuss how ignore_user _ uses the PHP function ignore_user_abort to implement scheduled tasks and continuous process instances through an instance with measurable results.

The second phase of the PHP built-in function research series uses the PHP function ignore_user_abort to implement scheduled tasks and continuous process instances. It also discusses the functions and usage of the ignore_user_abort () function through an instance that can detect the results.

Ignore_user_abort () can be used to execute PHP code after the client is closed. it can keep the PHP process running and implement the so-called scheduled task function and continuous process. you only need to enable the execution script, unless the apache server is restarted or the script has output, the PHP script will remain in the running state. it is very useful at first glance, but the cost is a continuous process of executing the script in PHP, large overhead, but can achieve many unexpected achievements.

It is described as setting whether to terminate the script execution when the client is disconnected.

I. function prototype
Int ignore_user_abort ([bool setting])

2. version compatibility
PHP 3> = 3.0.7, PHP 4, PHP 5

III. basic function usage and examples
1. basic function usage

Ignore_user_abort ();
?>

Note: Call the ignore_user_abort () function to declare that the script is not terminated even if the client is disconnected.

2. use the set_time_limit () function to execute a cyclic script.

Ignore_user_abort ();
Set_time_limit (0 );
$ Interval = 60*15;
Do {
// Executed business
} While (true );
?>


Note: it is executed cyclically every 15 minutes.

3. output the custom implementation file and track the execution result of the ignore_user_abort () function.

Ignore_user_abort (TRUE );
Set_time_limit (0 );
$ Interval = 10;
$ Stop = 1;
Do {
?? ? If ($ stop = 10) break;
?? ? File_put_contents ('liuhui. php', 'Current Time: '. time (). 'Stop:'. $ Stop );
?? ? $ Stop ++;
?? ? Sleep ($ interval );
} While (true );
?>

Open the liuhui. php file. the file content is as follows:
? Current Time: 1273735029 Stop: 9

The principle is that, even if the client terminates the script, it still runs once every 10 seconds and prints the current time and end point, so that the specific effect of the ignore_user_abort () function can be tested.

It is found through the instance that the ignore_user_abort () function is very practical, and it is very effective to implement the scheduled task, complete the subsequent task, and continue the process. For more information, see The PHP Manual. Please pay attention to the next PHP built-in function Research Series.

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.