The Redemption of PHP scheduled tasks

Source: Internet
Author: User
Implementation of the PHP scheduled task

Implementation of PHP Scheduled Tasks (ZT)
Article Category: PHP programming
Ignore_user_abort (); PHP scripts can continue to execute even if the client disconnects (such as turning off the browser).
Set_time_limit (0); Execution time is unlimited, php default execution time is 30 seconds, through Set_time_limit (0) can allow the program to execute indefinitely
$interval =60*5; Run every 5 minutes
do{
$fp = fopen (' test.txt ', ' a ');
Fwrite ($fp, ' test ');
Fclose ($FP);
Sleep ($interval); Wait 5 minutes
}while (TRUE);
?>


PHP Timing Schedule Task Introduction 2010-05-06 10:10 before the SE is particularly interested, but they do not java,lucene and other search engine development tools, so constantly digging PHP efficiency.

Finally found that PHP can also do crawl, and the principle is easy: Get the page source file directly, and then through the regular or string reference interception to obtain the required information. But the performance can not be compared with the search engine multi-threaded crawl.

After implementing the previous step, and then thinking, if the crawl can be automatically timed to get, then manually run the executable page will be saved.

Later also in some PHP open-source program to learn about the "scheduled task" effect: You can run a program on a regular basis, such as database backup, update cache, generate static pages, generate site map and so on.

Recently because the project needs to regularly update the remote database to the local, search the internet, and really found.

Ignore_user_abort (), function with Set_time_limit (0), and sleep ($interval), to achieve the above automatic update.

First give a basic paradigm in which there are individual test procedures:

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);
?>

First run the program, then close the page, the program is still running, and test will fill the Text3.txt file every 30 seconds.

Implementation effect (slightly)

Finally, according to the PHP manual a brief introduction of some relevant knowledge:

1. Connection handling:

Inside PHP, the system maintains a connection state with three possible conditions:

0-normal (Normal)
1-aborted (Abnormal exit)
2-timeout (Timeout)

The connection is valid when the PHP script is running normally in normal state. When the remote client disconnects, the token for the aborted status is opened. The interruption of a remote client connection is usually caused by the user tapping the STOP button. When the connection time exceeds the time limit of PHP, the TIMEOUT status token will be opened.

You can decide whether the script needs to exit when the client disconnects. Sometimes it is convenient to have the script run completely, even if no remote browser accepts the output of the script. The default scenario is that the script exits when the remote client connection is interrupted. This process can be controlled by the ignore_user_abort of the php.ini or by the corresponding "Php_value ignore_user_abort" and the Ignore_user_abort () function in the Apache conf settings. If you do not tell PHP to ignore the user's interruption, the script will be interrupted unless the shutdown trigger function is set by Register_shutdown_function (). With this shutdown trigger function, when the remote user taps the STOP button and the script attempts to output the data again, PHP will detect that the connection has been interrupted and invoke the shutdown trigger function.

Scripts can also be interrupted by a built-in script timer. The default time-out limit is 30 seconds. This value can be changed by setting the "Php_value max_execution_time" parameter or the Set_time_limit () function in the php.ini max_execution_time or Apache conf settings. When the counter times out, the script will exit similar to the above connection interruption, and the previously registered shutdown trigger function will be executed at this time. In the shutdown trigger function, you can check whether the timeout causes the shutdown trigger function to be called by calling the Connection_status () function. If the timeout causes a call to close the trigger function, the function returns 2.

One thing to note is that the aborted and TIMEOUT states can be valid at the same time. This is possible when telling PHP to ignore the user's exit operation. PHP will still be aware of the situation where the user has interrupted the connection but the script is still running. If the run time limit is reached, the script will be exited and the set off trigger function will be executed. At this point, the function connection_status () is found to return 3.

2. Related functions:

int Ignore_user_abort ([bool setting])
This function sets whether a client disconnect should cause a script to be aborted. It'll return the previous setting and can be called without a argument to don't change the current setting and only retur n the current setting.

int connection_aborted (void)
Returns TRUE if client disconnected.

int connection_status (void)
Returns the connection status Bitfield.


This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/webdesman/archive/2009/09/02/4510372.aspx
?

  • 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.