Question

Hi is there a possibility to set time limit only to a command or only to a function eg:

function doSomething()
{
    //..code here..

    function1();

    //.. some code here..

}

I want to set time limit only to function1.

There exits set_time_limit but I think this sets the time limit to whole script. Anybody any Idea?

Was it helpful?

Solution

set_time_limit() does run globally, but it can be reset locally.

Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.

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.

I've not tested it, but you may be able to set it locally, resetting when you leave the

<?php
set_time_limit(0);  // global setting

function doStuff()
{
    set_time_limit(10);   // limit this function
    //  stuff
    set_time_limit(10);   // give ourselves another 10 seconds if we want
    //  stuff
    set_time_limit(0);    // the rest of the file can run forever
}

// ....
sleep(900);
// ....
doStuff();  // only has 10 secs to run
// ....
sleep(900);
// ....

set_time_limit() ... 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.

OTHER TIPS

You would need to code that yourself if you want function1 to return when the time limit is up.

function doStuff($seconds)
{
    $finish=time()+$seconds;

    $done=false;
    $timeout=false;

    while (!$done && !$timeout)
    {
        //do stuff here, set $done to true if you complete

        //out of time?
        $timeout=time() >= $finish;

    }

    return $done; //let caller know if we completed
}

Of course, the "stuff" that you do needs to be iterative so that you can keep checking on your elapsed time and quit if necessary.

If you want timeout execution of an arbitrary function and have control returned to you, that is not supported. If you really need to do this, engineer it as a separate process. Then you can close the process if it seems to be taking too long.

$endtime=time()+10;

while(time()!=$endtime){

    echo ".";
}

No, it is not possible : max_execution_time is used as a security measure, to avoid having buggy scripts (like infinite loop) destroy the server ; but it is not intended to work on a function-unit basis.

And the fact that it ends up with a Fatal Error is definitly not nice for the user anyway...

I use exec to run shellscript that generates export from external application. If script runs too long, max_execution_time is reached and user will never know what happened. Limitation to such function would help me determine if end is near and send some user friendly error.

i dont think that this is possible, but why would you do this? If a script ever gets interrupted i might not do want you want it to. This should be more like a protection from yourself (just in case you upload an endless loop or something that will take hours), but if your script takes lots of time, you should either give it the time that it takes, or change the script so that it runs faster (for some crons its ok to take a while, but if a normal script that is called quite often takes more then the default time limit, you might face some serious performance problems)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top