I've a script to send packages of 300-500 e-mails hour. That means that this script will be fired once a hour using cron or other feature.
The server has a max execution limit of 30secs and it's not configurable.
I've been thinking if the pseudo-code below should work:
$time=time();
$count=0;
while(condition){
$count++;
send(email);
$now=time();
if($now-$time>=29){break;} //1sec margin
}
echo "$count e-mails sent";
Opinions?
if your script is launched with cron it means that you're using PHP-CLI "PHP Command Line Interface".
As mentioned in the PHP documentation, your have no time limit while using CLI.
So you don't have to worry about that : max_execution_time
is set to unlimited
.
Just to double-check that you can't set the execution time, here are two suggestions.
You could simply call set_time_limit()
before sending an e-mail. According to the PHP docs:
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.
For instance:
foreach ($emails as $email) {
set_time_limit(30);
send($email, ...);
}
Another option is via the cron. Since you are running PHP from a cron job, you can specify your own php.ini
. You could execute your script as follows:
php -c /custom/directory/my_php.ini my_script.php
Where my_php.ini
may specify:
max_execution_time = 0 ; (unlimited)
Break up the task in smaller chunks. Use the database to keep "state" of the actual job execution.
This approach has the advantage of being scalable: you probably will end-up having to send more emails as you grow, won't you?
精彩评论