PHP: ignore_user_abort () + set_time_limit (0) + sleep ($ interval) implement scheduled tasks

Source: Internet
Author: User
PHP: ignore_user_abort () + set_time_limit (0) + sleep ($ interval) to implement scheduled tasks recently because the project needs to regularly update the remote database to the local device, so I searched the internet and found it. The ignore_user_abort () function is used with set_time_limit (0) and sleep ($ int PHP: ignore_user_abort () + set_time_limit (0) + sleep ($ interval) to implement scheduled tasks.

Recently, because the project needs to regularly update the remote database to a local location, I found it online.

Ignore_user_abort ()Function combination Set_time_limit (0)And Sleep ($ interval)You can achieve the above automatic updates.

First, a basic paradigm is given, with a personal test program:
 


First run the program, then close the page, and then fill in the text3.txt file every 30 seconds when the program is still running.
Finally, some related knowledge is briefly introduced according to the php manual:
1. connection handling:
In PHP, the system maintains the connection status. There are three possible states:
0-NORMAL (NORMAL)
1-ABORTED (exit unexpectedly)
2-TIMEOUT (TIMEOUT)

When the PHP script runs normally, the connection is valid. When the remote client is disconnected, the ABORTED status mark will be opened. The interruption of remote client connection is usually caused by the user clicking the STOP button. When the connection time exceeds the PHP time limit, the TIMEOUT status mark will be opened.

You can determine whether the script needs to exit when the client terminates the connection. Sometimes it is much more convenient to run the script completely, even if the script output is not accepted by a remote browser. By default, the script will exit when the remote client connection is interrupted. This process can be controlled by php. ini's ignore_user_abort or by the "php_value ignore_user_abort" and ignore_user_abort () functions in Apache. conf settings. If PHP is not told to ignore user interruptions, the script will be interrupted unless the trigger function is disabled through register_shutdown_function. When a remote user clicks the STOP button and the script tries to output data again, PHP will detect that the connection has been interrupted and call to disable the trigger function.

The script may also be interrupted by the built-in script timer. The default timeout limit is 30 seconds. This value can be changed by setting the "php_value max_execution_time" parameter in the max_execution_time or Apache. conf setting of php. ini or the set_time_limit () function. When the counter times out, the script will exit when the above connection is interrupted, and the previously registered closed trigger function will also be executed at this time. In this function, you can call the connection_status () function to check whether timeout has caused the function to be called. If timeout causes function call to be disabled, the function returns 2.
Note that the ABORTED and TIMEOUT statuses are valid at the same time. It is possible to tell PHP to ignore the user's exit operation. PHP will still note that the user has interrupted the connection but the script is still running. If the running time limit is reached, the script will be exited, and the set function to close and trigger will also be executed. The connection_status () function returns 3.

II. related functions:
Int ignore_user_abort ([bool setting])
This function sets whether a client disconnect shocould cause a script to be aborted. it will return the previous setting and can be called without an argument to not change the current setting and only return the current setting.
Int connection_aborted (void)
Returns TRUE if client disconnected.
Int connection_status (void)
Returns the connection status bitfield.



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.