2

set_time_limitが30秒に設定されていて、変更できないとしましょう。1つのcronジョブで30秒より長く実行されるPHPの電子メールスクリプトがあります(まだそのタイムマークに達していません)。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
}
4

2 に答える 2

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;
于 2012-07-23T21:49:23.057 に答える
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秒ごとに実行します。可能な限り送信し、残りは次回送信されます。

于 2012-07-23T22:10:01.250 に答える