Limit Execution Time of an Function or Command PHP

Limit execution time of an function or command PHP

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.

PHP: Limit execution time per function

You cannot limit execution time by function in PHP. However, do not despair: if as you mentioned your function reads URL, you could use curl extension, which has options set options via curl_setopt, as following:

CURLOPT_TIMEOUT The maximum number of seconds to allow cURL functions to execute.
CURLOPT_CONNECTTIMEOUT The number of seconds to wait while trying to connect.

Using these you could limit the actual time spent on the URL processing with curl.

You could also use http extension which also allows you to do http connections and has timeout options.

Finally, you could use context options to file_get_contents:

$opts = array('http' =>
array(
'timeout' => 1.5 // 1.5 seconds
)
);

$context = stream_context_create($opts);

$result = file_get_contents('http://example.com/getdata.php', false, $context);

Can I define time limit for one function in PHP?

You can set time limit for the whole PHP script. So you can move out this function to separate script, set time limit there and run it as sub-process.

I recommend using Symfony Process component (can be used without Symfony):

http://symfony.com/doc/current/components/process.html

Using it you can even set time limit from your main process:

http://symfony.com/doc/current/components/process.html#process-timeout

php set_time_limit, what's the exact behavior?

Quoting from this source:

The set_time_limit can be used to dynamically adjust the maximum execution time permitted to a script. It allows specifying the time in seconds and limits the script execution time to that many seconds.

The set_time_limit function whenever called, effectively extends the script execution time by that many seconds. So if the script has already run for 15 seconds and set_time_limit(30) is called, then it would run for a total of 30+15 = 45 seconds. That's how its designed to work.

Set max_execution_time for specific controller in symfony2

You can disable the PHP timeout limit with the set_time_limit function. More info here

as Example:

class TaskController extends Controller
{

public function longTaskAction()
{
set_time_limit(0); // 0 = no limits
// ..
}
}


Related Topics



Leave a reply



Submit