Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
记录PHP脚本的实时运行时间_Php_Email_Loops_Cron - Fatal编程技术网

记录PHP脚本的实时运行时间

记录PHP脚本的实时运行时间,php,email,loops,cron,Php,Email,Loops,Cron,假设我的set\u time\u limit设置为30秒,无法更改。我有一个用php编写的电子邮件脚本,在1个cron作业上运行的时间将超过30秒(我还没有达到那个时间点)。30秒后超时会发生什么?脚本会停止然后继续吗 如果没有,我想记录从脚本循环开始到达到30秒所经过的时间,并暂停该过程,然后继续 做这件事的好方法是什么 更新:我认为可行的方法 function email() { sleep(2); //delays script 2 seconds (time to break scr

假设我的
set\u time\u 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;

你问的是不可能的。当达到脚本超时时,它将停止。您不能暂停它并继续它。您只能重新启动它。让我告诉你我做了什么来解决一个类似的问题

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秒运行一次。它发送它能发送的,然后下次发送其余的。

您尝试了哪些方法?30秒后,将发出致命错误,脚本将终止@没有什么我有信心的。。出于这个原因,我在寻求指导。你在寻找什么?当然也有关于如何在一定时间限制下使用PHP完成批处理任务的教程。@CyberJunkie超时是针对整个脚本的,而不是针对函数的。再次调用该函数不会规避限制。尝试使用cron多次运行同一个脚本。如果您的PHP>=5.0,您可以使用
microtime(true)
作为float进行检索。谢谢您的建议!我正在考虑一种类似的方法,但没有检查时间的流逝。如果我每分钟都在运行脚本,我看不出有什么意义。这是因为你最初的问题。考虑如果你没有检查时间过去,发送一封电子邮件,但是脚本在发送电子邮件之前被中止。此人将收到一封重复的电子邮件。还要考虑主机可能会更改超时值,或者在另一台主机上使用脚本。检查已用时间让您可以控制并提前停止脚本,而不是依赖超时来结束脚本。