set_time_limit
(PHP 4, PHP 5, PHP 7)
set_time_limit — Ограничение времени выполнения скрипта
Описание
$seconds
)Задает время в секундах, в течение которого скрипт должен завершить работу. Если скрипт не успевает, вызывается фатальная ошибка. По умолчанию дается 30 секунд, либо время, записанное в настройке max_execution_time в php.ini (если такая настройка установлена).
При вызове set_time_limit() перезапускает счетчик с нуля. Другими словами, если таймаут изначально был 30 секунд, и через 25 секунд после запуска скрипта будет вызвана функция set_time_limit(20), то скрипт будет работать максимум 45 секунд.
Список параметров
-
seconds
-
Максимальное время выполнения в секундах. Если задан ноль, время выполнения неограничено.
Возвращаемые значения
Возвращает TRUE
в случае успеха, иначе FALSE
.
Примечания
Эта функция не работает, если PHP работает в безопасном режиме (безопасный режим). Обойти это ограничение можно только выключив безопасный режим или изменив значение настройки в php.ini.
Замечание:
Функция set_time_limit() и директива max_execution_time влияют на время выполнения только самого скрипта. Время затраченное на различные действия вне скрипта, такие как системные вызовы функции system(), потоковые операции, запросы к базам данных и т.п. не включаются в расчет времени выполнения скрипта. Это не относится к Windows системам, где расчитывается абсолютное время выполнения.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Изменение поведения PHP
- PHP Опции и Информация
- assert_options
- assert
- cli_get_process_title
- cli_set_process_title
- dl
- extension_loaded
- gc_collect_cycles
- gc_disable
- gc_enable
- gc_enabled
- gc_mem_caches
- get_cfg_var
- get_current_user
- get_defined_constants
- get_extension_funcs
- get_include_path
- get_included_files
- get_loaded_extensions
- get_magic_quotes_gpc
- get_magic_quotes_runtime
- get_required_files
- get_resources
- getenv
- getlastmod
- getmygid
- getmyinode
- getmypid
- getmyuid
- getopt
- getrusage
- ini_alter
- ini_get_all
- ini_get
- ini_restore
- ini_set
- magic_quotes_runtime
- main
- memory_get_peak_usage
- memory_get_usage
- php_ini_loaded_file
- php_ini_scanned_files
- php_logo_guid
- php_sapi_name
- php_uname
- phpcredits
- phpinfo
- phpversion
- putenv
- restore_include_path
- set_include_path
- set_magic_quotes_runtime
- set_time_limit
- sys_get_temp_dir
- version_compare
- zend_logo_guid
- zend_thread_id
- zend_version
Коментарии
When you are working with IIS, PHP timeout is valid only when it's lower than script timeout defined by IIS.
IIS 5 has a default timeout of 300 seconds. If you need a higher timeout, you also have to change IIS properties. Otherwise, your server will stop your PHP script before it reaches its own timeout.
You may also need to look at Apache's timeout setting (Win32 version for me), I changed max execution time value in php.ini, and still got stopped by Apache's timeout value in the httpd.conf file.
I was having trouble with script timeouts in applications where the user prompted long running background actions. I wrote this cURL/CLI background script that solved the problem when making requests from HTTP.
<?php
/* BACKGROUND CLI 1.0
eric pecoraro _at_ shepard dot com - 2005-06-02
Use at your own risk. No warranties expressed or implied.
Include this file at the top of any script to run it in the background
with no time limitations ... e.g., include('background_cli.php');
The script that calls this file should not return output to the browser.
*/
# REQUIREMENTS - cURL and CLI
if ( !function_exists('curl_setopt') OR !function_exists('curl_setopt') ) {
echo 'Requires cURL and CLI installations.' ; exit ;
}
# BUILD PATHS
$script = array_pop(explode('/',$SCRIPT_NAME)) ;
$script_dir = substr($SCRIPT_NAME,0,strlen($SCRIPT_NAME)-strlen($script)) ;
$scriptURL = 'http://'. $HTTP_HOST . $script_dir . "$script" ;
$curlURL = 'http://'. $HTTP_HOST . $script_dir . "$script?runscript=curl" ;
# Indicate that script is being called by CLI
if ( php_sapi_name() == 'cli' ) {
$CLI = true ;
}
# Action if script is being called by cURL_prompt()
if ( $runscript == 'curl' ) {
$cmd = "/usr/local/bin/php ".$PATH_TRANSLATED ; // server location of script to run
exec($cmd) ;
exit;
}
# USER INTERFACE
// User answer after submission.
if ( $post ) {
cURL_prompt($curlURL) ;
echo '<div style="margin:25px;"><title>Background CLI</title>';
echo 'O.K. If all goes well, <b>'.$script.'</b> is working hard in the background with no ' ;
echo 'timeout limitations. <br><br><form action='.$scriptURL.' method=GET>' ;
echo '<input type=submit value=" RESET BACKGROUND CLI "></form></div>' ;
exit ;
}
// Start screen.
if ( !$CLI AND !$runscript ) {
echo '<title>Background CLI</title><div style="margin:25px;">' ;
echo '<form action='.$scriptURL.' method=POST>' ;
echo 'Click to run <b>'.$script.'</b> from the PHP CLI command line, in the background.<br><br>' ;
echo '<input type=hidden value=1 name=post>' ;
echo '<input type=submit value=" RUN IN BACKGROUND "></form></div>' ;
exit ;
}
# cURL URL PROMPT FUNCTION
function cURL_prompt($url_path) {
ob_start(); // start output buffer
$c=curl_init($url_path);
curl_setopt($c, CURLOPT_TIMEOUT, 2); // drop connection after 2 seconds
curl_exec($c);
curl_close($c);
ob_end_clean(); // discard output buffer
}
?>
this work to fine html streaming AND time pass limit
<?php
header('Content-type: text/plain');
echo date("H:m:s"), "\n";
set_time_limit(30);
for ($i = 0; $i < 1000; $i++)
{
echo date("H:m:s"),"\n";
for ($r = 0; $r < 100000; $r++){
$X.= tan(M_LNPI+log(ceil( date("s")*M_PI*M_LNPI+100)));
}
ob_flush();
flush();
}
echo "work! $x";
?>
If you are streaming large data from database, it is counted towards the max exec time.
while setting the set_time_limit(), the duration of sleep() will be ignored in the execution time. The following illustrates:
<?php
set_time_limit(20);
while ($i<=10)
{
echo "i=$i ";
sleep(100);
$i++;
}
?>
Output:
i=0 i=1 i=2 i=3 i=4 i=5 i=6 i=7 i=8 i=9 i=10
To find out the currently set time limit, use
<?php
ini_get('max_execution_time');
?>
If set_time_limit has been previously called in the script, the result will be the value which was passed to set_time_limit (and not, as the function name "ini_get" appears to suggest, the value from the php.ini file).
In IIS, there is another global timeout setting which will override any PHP settings. You can alter this timeout by following the following instructions:
http://www.iisadmin.co.uk/?p=7
You can do set_time_limit(0); so that the script will run forever - however this is not recommended and your web server might catch you out with an imposed HTTP timeout (usually around 5 minutes).
You should check your web server's guides for more information about HTTP timeouts.
Jonathon
To get the currently used time, use getrusage()
One thing that I wish I had found sooner is, if you're using php-cli and really need to limit the executation time, and if you're in *nix, you can use "timeout" which is part of coreutils.
For example:
timeout 5 /usr/bin/php -q /path/to/script
and it will kill it if it takes longer than 5 seconds.
I had a few quick php scripts I wrote for use with cacti for example.
Documentation states:
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.
If I have a long running script and i want a exact time limit, I set this as near as possible to the first line.
Both set_time_limit(...) and ini_set('max_execution_time',...); won't count the time cost of sleep,file_get_contents,shell_exec,mysql_query etc, so i build this function my_background_exec(), to run static method/function in background/detached process and time is out kill it:
my_exec.php:
<?php
function my_background_exec($function_name, $params, $str_requires, $timeout=600)
{$map=array('"'=>'\"', '$'=>'\$', '`'=>'\`', '\\'=>'\\\\', '!'=>'\!');
$str_requires=strtr($str_requires, $map);
$path_run=dirname($_SERVER['SCRIPT_FILENAME']);
$my_target_exec="/usr/bin/php -r \"chdir('{$path_run}');{$str_requires} \\\$params=json_decode(file_get_contents('php://stdin'),true);call_user_func_array('{$function_name}', \\\$params);\"";
$my_target_exec=strtr(strtr($my_target_exec, $map), $map);
$my_background_exec="(/usr/bin/php -r \"chdir('{$path_run}');{$str_requires} my_timeout_exec(\\\"{$my_target_exec}\\\", file_get_contents('php://stdin'), {$timeout});\" <&3 &) 3<&0";//php by default use "sh", and "sh" don't support "<&0"
my_timeout_exec($my_background_exec, json_encode($params), 2);
}
function my_timeout_exec($cmd, $stdin='', $timeout)
{$start=time();
$stdout='';
$stderr='';
//file_put_contents('debug.txt', time().':cmd:'.$cmd."\n", FILE_APPEND);
//file_put_contents('debug.txt', time().':stdin:'.$stdin."\n", FILE_APPEND);
$process=proc_open($cmd, [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'w']], $pipes);
if (!is_resource($process))
{return array('return'=>'1', 'stdout'=>$stdout, 'stderr'=>$stderr);
}
$status=proc_get_status($process);
posix_setpgid($status['pid'], $status['pid']); //seperate pgid(process group id) from parent's pgid
stream_set_blocking($pipes[0], 0);
stream_set_blocking($pipes[1], 0);
stream_set_blocking($pipes[2], 0);
fwrite($pipes[0], $stdin);
fclose($pipes[0]);
while (1)
{$stdout.=stream_get_contents($pipes[1]);
$stderr.=stream_get_contents($pipes[2]);
if (time()-$start>$timeout)
{//proc_terminate($process, 9); //only terminate subprocess, won't terminate sub-subprocess
posix_kill(-$status['pid'], 9); //sends SIGKILL to all processes inside group(negative means GPID, all subprocesses share the top process group, except nested my_timeout_exec)
//file_put_contents('debug.txt', time().":kill group {$status['pid']}\n", FILE_APPEND);
return array('return'=>'1', 'stdout'=>$stdout, 'stderr'=>$stderr);
}
$status=proc_get_status($process);
//file_put_contents('debug.txt', time().':status:'.var_export($status, true)."\n";
if (!$status['running'])
{fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return $status['exitcode'];
}
usleep(100000);
}
}
?>
a_class.php:
<?php
class A
{
static function jack($a, $b)
{sleep(4);
file_put_contents('debug.txt', time().":A::jack:".$a.' '.$b."\n", FILE_APPEND);
sleep(15);
}
}
?>
test.php:
<?php
require 'my_exec.php';
my_background_exec('A::jack', array('hello', 'jack'), 'require "my_exec.php";require "a_class.php";', 8);
?>
set_tme_limit resets the execution time count.
test code1:
<?php
echo '<html><body>';
set_time_limit(1);
$i = 0;
while(++$i < 100000001){
if($i % 100000 == 0){
echo $i / 100000, "<br/>\n";
}
}
echo "done.<br/>\n";
// will not echo 'done.'.
?>
test code2:
<?php
echo '<html><body>';
set_time_limit(1);
$i = 0;
while(++$i < 100000001){
if($i % 100000 == 0){
set_time_limit(1);
echo $i / 100000, "<br/>\n";
}
}
echo "done.<br/>\n";
// will echo 'done.'
?>
if you want to check how much time remains, this should work (at least on Windows, on non-Windows platforms, i'm not sure)
$seconds_remaining_until_termination = ini_get('max_execution_time') === "0" ? null : ((int)ini_get('max_execution_time'))-(microtime(true)-$_SERVER['REQUEST_TIME_FLOAT']);
gives you number of seconds until the script will be terminated due to the time limit. (tested on Windows 7 X64 SP1 with PHP 7.3.7) - or gives you null if there is no time limit.
If you would like to calculatue the wall time of your script (includes all external DB/HTTP calls etc.) in Unix (in Windows this is already default behavior), you could use the following function:
<?php
$timeoutInSeconds = 3;
// This will make sure this is always called async
pcntl_async_signals(1);
// Second parameter is any callable (https://www.php.net/manual/en/language.types.callable.php)
pcntl_signal(SIGALRM, function() {
exit('Stop it!');
});
pcntl_alarm($timeoutInSeconds);
?>