views:

1199

answers:

6

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?

+2  A: 

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.

Paul Dixon
Thanx for the answer but look at this acenariofunction doStuff($seconds){ $finish=time()+$seconds; $done=false; $timeout=false; while (!$done this function is a standart php function which can not be modified and it takes too much to execute in my case. $timeout=time() >= $finish; } return $done; //let caller know if we completed}
Granit
Thanx for the answer but look at this acenario <code>function doStuff($seconds) { $finish=time()+$seconds; $done=false; $timeout=false; while (!$done this function is a standart php function which can not be modified and it takes too much to execute in my case. $timeout=time() >= $finish; } return $done; //let caller know if we completed }</code>
Granit
You can't have an arbitrary function quit after a specified time. 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.
Paul Dixon
A: 

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)

Flo
Lets say that my scripts gets data from database and for each row it tries to do something with that row likedoSomething(row)but sometimes this could be too much time consuming.So I need something like when this doSomething is not finished in 5 minutes to continue with the other row.If somehow I could make a function like :function doSomething(){//code herereturn results;}But if this function can not be completed in 5 minutes to return false for example!
Granit
ah ok, in that case stick with the answer of paul dixon
Flo
+1  A: 

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.

Alister Bulman
Very nice! thanx! but it returns a fatal error if the time criteria was not met.Is there a way to avoid that.
Granit
It's not possible to stop the fatal error - as that is what the timeout is. If you want to do something else, then you will have to define more fully what you are doing, and design a system that will perform as required.If you are doing database queries that are taking too long, I would try to improve the query (and underlying DB) to speed them up, rather than trying to fix somethough outside the original problem.
Alister Bulman
Thanx you helped me find the right thing.I think what I was looking was max_input_timeDo you know if it returns also fatal error if the deadline is not met?
Granit
the php.ini config, max_input_time, is how long a script takes to start-up, parsing input data, like POST, GET and file uploads. These tasks must completed before any of your PHP code is run.
Alister Bulman
A: 

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...

Pascal MARTIN
A: 

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

Lukas
A: 

It didnot work i've just tried this code

<?php
    set_time_limit(0);
    function showInfinite(){
        set_time_limit(5);
        $i=1;
        while(true){
            echo "I:".$i++."<br>";
            sleep(1);
        }   
        set_time_limit(10);
        for($j=1;$j<=100;$j++){
            echo "J:".$j."<br>";
        }
    }

    showInfinite();
?>

It only shows the value of I up to 5 and then the script stop

silence ghost