PHP: Maximum execution time of 30 seconds exceeded, continue looping - php

I'm writing a a php script where i do a loop and inside the loop there is a particular function processing information, as illustrated here.
while (some condition){
// some code
processInformation();
// Some more code
}
Well it turns out that midway, inside processInformation(), I encountered the error Fatal error: Maximum execution time of 30 seconds exceeded. Using set_time_limit(0), would not help in my situation because processInformation() might take forever. I don't want an error to completely break my program, so is there anyway i can catch an exception or tell php to "continue" the loop if processInformation() takes too long?

You can increase the value of maximum execution time from 30 seconds to any value. For example: if you want to set the limit of maximum execution time of 3 minutes (instead of 30 seconds), then you can set as:
ini_set('max_execution_time', 180); //maximum execution time of 3 minutes
This way, you can set the time based on your need.

I dunno how to catch the exception but you could use a simple time calculation to escape after say 60 secs. ()
$x=time();
while (some condition){
processInformation();
if ($x + 60) < time()
break;
}

Related

PHPExcel for loop timeout

I have this code where I want to set from 3 to 333 to have a certain row height at every 30 interval, meaning at row 3, 33, 63, 93 up till 333:
for ($i=3; $i<340; $i+30){
$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(57);
}
But when I click on controller actionGenerate(), it doesn't generate and timeouts me after 30 seconds. Can I know why isn't it generating?
If I were to use the manual way, as in doing line by line, PLUS having so many different kinds of settings, I'd be exhausted.
PHP limits the execution time of any script. The default time is 30 seconds.
Your script probably takes longer than 30 seconds to execute.
Set your set_time_limit to 0.
This will set it to unlimited.
like this:
set_time_limit(0);
for ($i=3; $i<340; $i+=30){ // <--- here was your problem
$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(57);
}
And try again.
Info about set_time_limit in the docs
-- edit--
The problem was in the for loop.
$i + 30 adds 30 to $i but it does not assign it.
It should be $i +=30

php set_time_limit less than one second

The question is simple, I would like to check a database to serve customised content to a site visitor, but failover and serve a generic page if this function takes more then 800ms to execute. (Target time for the server response is 1000ms).
I've seen the set_time_limit function, however this takes an integer in seconds as the argument.
My question: is there something similar that can be used with values of less than 1 second?
I'm looking for something like:
void set_time_limit_ms ( int $milliseconds )
set_time_limit_ms (800)
doesn't exist. you just could emulate it with a tick function:
declare(ticks=1); // or more if 1 takes too much time
$start = microtime(1);
register_tick_function(function () use ($start) {
(microtime(1) - $start < 0.8) or die();
});
You will not be able to use this function to prevent a query that is running longer than you expected. This only measures the actual script execution time. Here is an bit from the manual.
The set_time_limit() function and the configuration directive
max_execution_time only affect the execution time of the script
itself. Any time spent on activity that happens outside the execution
of the script such as system calls using system(), stream operations,
database queries, etc. is not included when determining the maximum
time that the script has been running. This is not true on Windows
where the measured time is real.
Yes there is, I often use microtime. see
http://php.net/manual/en/function.microtime.php
<?php
$time_start = microtime(true);
// Sleep for a while
usleep(100);
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
?>

random delay timer in PHP

I want to put a php daemon to sleep (with System_Daemon::iterate())
so it runs max 20 times randomly spread over an hour. maybe a min distance would be smart so it doesn't run 20 times in the first half hour and 0 times in the second half.
i'm kinda stuck here and don't know how to start with this one, any help is very apreciated!
You may use cron jobs, to set the script to run ever so often.
http://net.tutsplus.com/tutorials/php/managing-cron-jobs-with-php-2/
... Crontab:
0 9 * * * /path/to/bashscript
and in /path/to/bashscript:
#!/bin/bash
maxdelay=$((1*60)) # every hour, converted to minutes
for ((i=1; i<=20; i++)); do
delay=$(($RANDOM%maxdelay)) # pick an independent random delay, 20 times
(sleep $((delay*60)); /path/to/phpscript.php) & # background a subshell, then run the php script
done
i came up with one possible solution, i didnt try it out yet, so it main contain syntax or logic errors. because it is running as a daemon there is a never ending loop around it.
// 3600 seconds or one hour
$timeframe=3600;
// run max 20 times in $timeframe
$runtimes=20;
// minimum delay between two executions
$mindelay=60;
// maxium delay between two executions
$maxdelay=240;
if ($cnt % $runtimes != 0) {
$delay = rand($mindelay,$maxdelay);
System_Daemon::iterate($delay);
$sum += $delay;
$cnt++;
} else {
//final delay till the $timeframe
if ($sum < $timeframe) {
System_Daemon::iterate($timeframe - $sum);
}
$sum=0;
}
its not perfect and u waste some time but i guess its going to fullfill the job.
any comments?

Record live elapsed time of PHP script

Say my set_time_limit is set to 30 seconds and can't be changed. I have an email script in php that will run longer than 30 seconds on 1 cron job (I haven't reached that time mark yet.) What happens on timeout after 30 seconds? Will the script stop then continue?
If not, I would like to record the elapsed time from the beginning of my script's loop until it reaches 30 seconds and pause the process then continue.
What is a good way to do this?
Update: what I think might work
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
}
Suggested approach:
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;
What you ask is not possible. When the script timeout is reached, it stops. You cannot pause it and continue it. You can only restart it. Let me tell you what I did to solve a similar problem.
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
Run this every 60 seconds. It sends what it can, then the rest gets sent next time.

Average sleep interval in PHP

I need to get average sleep interval from following data:
22:00-06:00
00:00-08:00
02:00-10:00
=> expected: 00:00-08:00
22:00-06:00
00:00-08:00
02:00-10:00
04:00-08:00
=> expected: 01:00-08:00
The problem is oscillation around midnight, where part is under 00:00 and part over 00:00.
Simple mean 22+00+02+04 doesn't work. I could count number of times over midnight (3 in this case) and if it's more than those before midnight, I should add 25 co compensate. But this doesn't count with those people, who work at night and go sleep around 8-14!
My theory is: First, somehow I need found peak, something like the most frequented area (e.g., in 9-10 there is 5 record, in 10-11, there is 3 etc.) and then I can decide co compensate border values by adding 24 hours.
What do you think?
What about taking into account relative difference with midnight ?
The result would be (-2+0+2+4)/4 = 00:45
Making the assumption that the person is not sleeping more than 24 hours, then make a method to calculate like so (pseudo code):
calculateSleepTime(sleepTime, wakeupTime) {
if (sleepTime > wakeupTime) {
return (24 - sleepTime) + wakeupTime;
} else {
return wakeupTime - sleepTime;
}
}
averageSleepTime(sleepTimesArray) {
totalSleptTime = 0;
totalTimesSlept = 0;
foreach (oneSleepTime in sleepTimesArray) {
totalTimesSlept++;
totalSleptTime += calculateSleepTime(oneSleepTime);
}
return totalSleptTime / totalTimesSlept;
}
After you get the average sleep time, calculate either the average sleep time, or average wake up time, and do addition/substraction to find your interval. The alternative to this is finding the average sleep time and the average wakeup time (taking relative to midnight times into account).
Determine a lower limit where you can say
Okay, people won't go to sleep this time of the day, if I get this time, it must mean they worked all night and only sleep now
and if the time is below that limit, add 24 hours to both start and finish.

Categories