PHP How to implement timed tasks, PHP Scheduled task method, the best solution, PHP automatic Task processing

Source: Internet
Author: User
Tags php script

PHP How to implement timed tasks, PHP Scheduled task method, the best solution, PHP automatic Task processingJoe PHP 2012-01-18

Timed tasks have always been a problem for many friends in PHP, but they have been encountered in many places.
For example:
Game development Program, every 10 minutes to the player looked so unfavorable
The SNS community detects every 20 seconds if someone sends me a message.
Automatic capture program, collect the latest content every 5 minutes
Micro-Blog Data synchronization, every 10 hours through the Micro-blog API interface synchronization of user data
......

There are probably several ways:
1.Linux system
You can use Cron to execute a PHP script or other language script directly [you need to have sufficient system privileges]
Perform an edit cron command

1 crontab-e

For example, every 5 minutes to execute the test.php script under the/root/bin directory

*/5 * * * * php/root/bin/test.php

2.windows system
Use scheduled tasks to execute scripts, no example

3. If you don't have system permissions, look down.
Since PHP is an interpreted weak language, it is not as easy as Java to do timed tasks, and a process in Java allows the system to perform timed tasks, but there is no such thing as a multi-threading concept in PHP.

The first time I contacted a timed task was a friend who did PHP to ask me how to implement the timing looked so unfavorable, I was given the answer is to write a containing file, each time the execution of the call this file to check whether there is a task, but imagine if no one triggered what to do, obviously the answer is not perfect.

Later I got the best solution through other channels, and I did the testing myself.
The general idea is to set the Shutdown browser program will also stay in the server memory execution, and the corresponding time never expires
See Program:

1234567891011121314151617181920212223242526272829303132 ignore_user_abort(TRUE);// 设定关闭浏览器也执行程序set_time_limit(0);      // 设定响应时间不限制,默认为30秒 $count = 0;while (TRUE){    sleep(5);           // 每5秒钟执行一次    // 写文件操作开始    $fp = fopen("test".$count.".txt", "w");    if($fp)    {        for($i=0; $i<5; $i++)        {            $flag=fwrite($fp,$i."这里是文件内容www.uacool.com\r\n");            if(!$flag)            {                echo "写入文件失败";                break;            }        }    }    fclose($fp);    // 写文件操作结束    $count++;    // 设定定时任务终止条件    if (file_exists(‘lock.txt‘))    {        break;    }}

Name this file tast.php in the root directory of the site
such as: www.uacool.com/tast.php
Visit this page. This program will then continue to generate the site root directory named Test0.txt,test1.txt,test2.txt ... The text file
After the visit can close the browser, do not affect the program to continue to execute, such as to block the program, please place a file named Lock.txt in the root directory of the Web site.
such as: Www.uacool.com/lock.txt
Of course, restarting the server or restarting the computer can also prevent the program from continuing to run

Related content:
schtasks Scheduled Tasks for Windows: http://www.cnblogs.com/lostyue/archive/2011/10/24/2223166.html

The cron service is a distributed timing service provided by the SAE for developers: http://sae.sina.com.cn/?m=devcenter&catId=195

PHP How to implement timed tasks, PHP Scheduled task method, the best solution, PHP automatic Task processing

Related Article

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.