Php: How to Implement scheduled tasks php: the best solution for scheduled tasks php automatic Task Processing

Source: Internet
Author: User
Tags server memory

Regular tasks have always been a problem for many friends in PHP, but they are encountered in many places.

For example:

In game development programs, send troops to players every 10 minutes

In the SNS community, check if someone sends me a message every 20 seconds.

The automatic collection program collects the latest content every 5 minutes.

Weibo Data Synchronization: user data is synchronized every 10 hours through the Weibo API

......

Because PHP is a weak interpreted language, it is not as easy as Java to perform scheduled tasks. In Java, a process can be directly assigned to the system to execute scheduled tasks, however, in PHP, there is no concept of multithreading in simple settings.

When I first came into contact with a scheduled task, a PHP friend asked me how to implement the scheduled task. The answer I gave at that time was to write a file containing the task, every time a task is executed, the file is called to check whether a task exists. However, if no task is triggered, the answer is obviously not perfect.

Later, I obtained the best solution through other channels, and I personally tested it.

The general idea is to set that the browser program will also stay in the server memory for execution, and the corresponding time will never expire.

Program Viewing:

Ignore_user_abort (TRUE); // set to close the browser and execute the program set_time_limit (0); // set the response time to 30 seconds. The default value is $ count = 0; while (TRUE) {sleep (5); // execute once every 5 seconds // The file write operation starts $ fp = fopen ("test ". $ count. ". txt "," w "); if ($ fp) {for ($ I = 0; $ I <5; $ I ++) {$ flag = fwrite ($ fp, $ I. "Here is the file content www.uacol.com \ r \ n"); if (! $ Flag) {echo "failed to write the file"; break ;}} fclose ($ fp); // end of the file write operation $ count ++; // set the timing task termination condition if(file_exists('lock.txt ') {break ;}}

Name this file tast. php In the root directory of the website.

For example: www.uacol.com/tast.php

Visit this webpage. This program will create another website root directory named test0.txt,test1.txt,test2.txt... Text File

You can close the browser after the program is closed, without affecting the execution of the program. If you want to stop the program, put a file named lock.txt in the website root directory.

Example: www.uacol.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 job for windows: http://www.cnblogs.com/lostyue/archive/2011/10/24/2223166.html

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

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.