PHP 3 ways to perform tasks in a timed manner, _php tutorial

Source: Internet
Author: User

PHP 3 ways to perform tasks in a timed manner,


Three ways to implement PHP timed execution

    • 1. Scheduled Tasks for Windows
    • 2, the Linux script program
    • 3. Let the Web browser refresh periodically

Specific implementation
1. Windows Scheduled Tasks

PHP rarely run on the win server, the specific implementation is no longer delve into, see the principle of online implementation is probably write bat script, and then let the window task add execute this bat script.
2, the Linux script implementation
Here the main use of the crontab command,
How to use:

Copy the Code code as follows: Crontab filecrontab [-u user] [-u user] {-l |-r |-e}

Description

Crontab is used to allow the user to execute the program at a fixed time or at a fixed interval.

Use Crontab to write shell scripts, and then let PHP call the shell, this is the use of Linux features, should not be considered the characteristics of PHP's own language

3, PHP implementation of scheduled tasks scheduled
Using PHP to make the browser refresh requires several issues to solve
PHP script Execution time limit, default is 30m solution: Set_time_limit (), or modify php.ini settings max_execution_time time (not recommended)
If the client browser shuts down, the program may be forced to terminate, workaround: Ignore_user_abort Even if the closing page still performs normally
If the program has been executing very likely to consume a large amount of resources, the workaround uses sleep to use the program to hibernate for a while and then perform
PHP timed Execution Code:

<?phpignore_user_abort ();//switch off the browser, the PHP script can also continue to execute. SET_TIME_LIMIT (3000);//Through Set_time_limit (0) You can allow the program to execute indefinitely. interval=5;//every 5s//method of the dead loop do{  echo ' Test '. Time (). '
'; Sleep ($interval);//wait 5s }while (true); Method 2---Sleep timed execution of require_once './curlclass.php ';//introduction file $curl = new Httpcurl ();//instantiation $stime = $curl Getmicrotime (); for ($i =0; $i <=10; $i + +) { echo ' Test '. Time (). '
'; Sleep ($interval);//wait 5s } Ob_flush (); Flush (); $etime = $curl->getmicrotime (); Echo '; Echo Round (($etime-stime), 4);//Program Execution time

The test found that the efficiency is not very high.

Summarize:

Personally feel that PHP is not very efficient to perform tasks on a regular basis, and it is recommended that you give it to the shell to perform tasks on a timed basis.

The above is PHP timed to perform the task of 3 methods, for the end of the previous article mentioned in the crontab This command, also carried out a brief introduction, I hope you can have some gains.

Articles you may be interested in:

    • PHP implementation code for scheduled tasks and scheduled tasks
    • Summary of various methods for PHP timed execution of scheduled tasks
    • How to implement timed scheduled tasks in PHP
    • How to implement PHP scheduling tasks
    • Linux uses crontab to implement scheduled tasks for PHP execution
    • How PHP Implements timed tasks

http://www.bkjia.com/PHPjc/1084586.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084586.html techarticle PHP 3 ways to perform tasks in detail, PHP timed execution of three ways to achieve 1, Windows Scheduled Task 2, the Linux script 3, let the Web browser timed refresh specific implementation of 1 、...

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