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