Comparison of several methods of implementing timed tasks in PHP

Source: Internet
Author: User
Several methods of implementing timed tasks in PHP

These days need to use PHP to write a timed crawl Web server application. Search on the Internet for a solution, found Oschina a question of the answer is wonderful

Several solutions are proposed.

I. Simple direct disregard of the consequences type

Ignore_user_abort ();//switch off the browser, the PHP script can also continue to execute. Set_time_limit (0);//Through Set_time_limit (0) You can allow the program to execute indefinitely $interval=60*30;/ /Run do{    //todo      sleep ($interval) every half hour;//wait 5 minutes}while (true);

Disadvantage: After booting, you cannot control it unless you terminate the PHP host. Do not use this method unless you are a hacker.

Two. Simple and controllable type

config.php

return 1;
 
  

cron.php

Ignore_user_abort ();//switch off the browser, the PHP script can also continue to execute. Set_time_limit (0);//Through Set_time_limit (0) You can allow the program to execute indefinitely $interval=60*30;/ /Every half hour run do{$run = include ' config.php ', if (! $run) die (' Process abort ');//todo  Sleep ($interval);//wait 5 minutes}while ( true);

The STOP program is implemented by changing the return 0 of the config.php. One way to do this is to config.php a file with a special form and set some variables to configure it via HTML pages.

Disadvantages: Accounting for system resources, long-running, there will be some unexpected hidden dangers. For example, memory management problems.

Three. Simple and improved type

$time =15; $url = "http://". $_server[' http_host '].$_server[' Request_uri '];/*    function*/sleep ($time); file_get_ Contents ($url);

The PHP script continues to execute after a period of time by accessing itself. It's like a relay race. This guarantees that each PHP script will not take too long to execute. It is not subject to time_out restrictions.

Because each time the PHP file is executed independently, this method avoids the time_out limitation. But it's best to add the control code to the top. Cofig.php so that the process can be terminated.

Four. CronTab type

Ccontroljobnum (1); while (true) {         
 
           
  
   <动态sleep>
    
                            } function Ccontroljobnum ($job _limit) {                 $cmd = @popen ("Ps-ef | grep ' {$_server[' script_filename '} ' | Grep-v grep | Wc-l ", ' R ');                 $num = @fread ($cmd, up to);                 $num + = 0;                 @pclose ($cmd);                 if ($num > $job _limit)                 {                         exit;                 }                 return true; }
  
   
 
  

The number of running processes is determined by Popen in the program to control concurrency

Five. Use the Cron service on BAE or SAE.

Six. Buy a VPS, with crontab

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