How to implement scheduled execution tasks in php _ PHP Tutorial

Source: Internet
Author: User
Implement the scheduled execution task method in php. PHP script execution time limit. the default solution is 30 m: set_time_limit (); or modify PHP. setting max_execution_time in ini (not recommended) to use php to refresh the browser must solve the PHP script execution time limit. the default solution is 30 MB: set_time_limit (); or modify PHP. set max_execution_time in ini (not recommended)

Several problems need to be solved to refresh the browser using php
If the browser of the client is closed, the program may be terminated. Solution: ignore_user_abort can still run normally even if the page is closed.
If the program runs continuously, it may consume a lot of resources. the solution is to use sleep to sleep the program for a while, and then run
PHP code regularly executed:

The code is as follows:

Ignore_user_abort (); // close the browser and run the PHP script.
Set_time_limit (3000); // you can use set_time_limit (0) to run the program without restrictions.
$ Interval = 5; // run every 5s

// Method 1 -- Endless loop
Do {
Echo 'test'. time ().'
';
Sleep ($ interval); // wait for 5 s
} While (true );

// Method 2 --- regular sleep execution
Require_once './curlClass. php'; // introduce the file

$ Curl = new httpCurl (); // instantiate
$ Stime = $ curl-> getmicrotime ();
For ($ I = 0; $ I <= 10; $ I ++ ){

Echo 'test'. time ().'
';
Sleep ($ interval); // wait for 5 s

}
Ob_flush ();
Flush ();
$ Etime = $ curl-> getmicrotime ();
Echo '';
Echo round ($ etime-stime), 4); // program execution time

Handler solution: set_time_limit (); or modify PHP. ini to set max_execution_time (not recommended). use php to refresh the browser...

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.