假设我的 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
}
最佳答案
建议的方法:
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;
关于php - 记录PHP脚本的实时运行时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11620989/