说我的set_time_limit
设置为30秒,无法更改。我在php中有一个电子邮件脚本,在1个cron作业上运行时间超过30秒(我还没有达到那个时间标记。)30秒后超时会发生什么?脚本会停止然后继续吗?
如果没有,我想记录从脚本循环开始到30秒的经过时间并暂停该过程然后继续。
这样做的好方法是什么?
更新:我认为可行的方法
function email()
{
sleep(2); //delays script 2 seconds (time to break script on reset)
foreach ($emails as $email):
// send individual emails with different content
// takes longer than 30 seconds
enforeach;
// on 28 seconds
return email(); //restarts process
}
答案 0 :(得分:1)
建议的方法:
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
foreach ($emails as $email):
// send individual emails with different content
// takes longer than 30 seconds
$time_curr = microtime_float();
$time = $time_curr - $time_start;
if($time > 28){ //if time is bigger than 28 seconds (2 seconds less - just in case)
break;// we will continue processing the rest of the emails - next time cron will call the script
}
enforeach;
答案 1 :(得分:1)
你问的是不可能的。达到脚本超时后,它将停止。你不能暂停它并继续它。你只能重新启动它。让我告诉你我为解决类似问题所做的工作。
Part One:
Queue all your emails into a simple database. (Just a few fields like to,
from, subject, message, and a flag to track if it is sent.)
Part Two.
1. Start script
2. Set a variable with the start time
3. Check the db for any mails that have not been sent yet
4. If you find any start a FOR loop
5. Check if more than 20 seconds have elapsed (adjust the window here, I like
to allow some spare time in case a mail call takes a few seconds longer.)
6. If too much time passed, exit the loop, go to step 10
7. Send an email
8. Mark this email as sent in the db
9. The FOR loop takes us back to step 4
10. Exit the script
每60秒运行一次。它发送它可以发送的东西,然后下次发送其余的。