Hướng dẫn does php have a timeout? - php có thời gian chờ không?

(Php 4, Php 5, Php 7, Php 8)

set_time_limit - giới hạn thời gian thực hiện tối đaLimits the maximum execution time

Sự mô tả

set_time_limit (int $seconds): bool(int $seconds): bool

Khi được gọi, set_time_limit () khởi động lại bộ đếm thời gian chờ từ 0. Nói cách khác, nếu thời gian chờ là 30 giây mặc định và 25 giây vào thực hiện tập lệnh, một cuộc gọi như set_time_limit(20) được thực hiện, tập lệnh sẽ chạy trong tổng số 45 giây trước khi hết thời gian.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.

Thông số

seconds

Thời gian thực hiện tối đa, tính bằng giây. Nếu được đặt thành 0, không có giới hạn thời gian được áp đặt.

Trả về giá trị

Trả về true khi thành công, hoặc false về thất bại.true on success, or false on failure.

Ghi chú

Ghi chú::

Hàm set_time_limit () và chỉ thị cấu hình MAX_EXECATED_TIME chỉ ảnh hưởng đến thời gian thực hiện của chính tập lệnh. Bất kỳ thời gian nào dành cho hoạt động xảy ra bên ngoài việc thực thi tập lệnh như các cuộc gọi hệ thống bằng hệ thống (), hoạt động luồng, truy vấn cơ sở dữ liệu, v.v. không được bao gồm khi xác định thời gian tối đa mà tập lệnh đã chạy. Điều này không đúng trên các cửa sổ trong đó thời gian đo là có thật.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.

kexianbin tại DIYism dot com ¶

8 năm trước

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:
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 pgidstream_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:
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:
require 'my_exec.php';my_background_exec('A::jack', array('hello', 'jack'), 'require "my_exec.php";require "a_class.php";', 8);
?>

mba_aslam tại yahoo dot com ¶

15 năm trước

while setting the set_time_limit(), the duration of sleep() will be ignored in the execution time. The following illustrates:

set_time_limit(20)0

set_time_limit(20)1

set_time_limit(20)2

Jonathon Dot Keogh tại Gmail Dot Com ¶

14 năm trước

set_time_limit(20)3

set_time_limit(20)4

set_time_limit(20)5

set_time_limit(20)6

Eric Pecoraro tại Shepard Com ¶

17 năm trước

set_time_limit(20)7

set_time_limit(20)8

set_time_limit(20)9

seconds0

seconds1

seconds2

Ẩn danh ¶

2 năm trước

seconds3

seconds4

seconds5

set_time_limit(20)6

Robertbrogers tại Gmail Dot Com ¶

8 năm trước

seconds7

seconds8

set_time_limit(20)6

mba_aslam tại yahoo dot com ¶

15 năm trước

true0

Jonathon Dot Keogh tại Gmail Dot Com ¶

14 năm trước

true1

true2

set_time_limit(20)6

Eric Pecoraro tại Shepard Com ¶

17 năm trước

true4

set_time_limit(20)0

true6

true7

Ẩn danh ¶

2 năm trước

true8

true9

false0

false1

Robertbrogers tại Gmail Dot Com ¶

PHP tại Mightycpa.com

false2

false3

false4

set_time_limit(20)6

19 năm trước

15 năm trước

false6

false7

set_time_limit(20)6

Jonathon Dot Keogh tại Gmail Dot Com ¶

15 năm trước

false9

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:0

set_time_limit(20)6

Jonathon Dot Keogh tại Gmail Dot Com ¶

14 năm trước

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:2

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:3

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:4

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:5

Eric Pecoraro tại Shepard Com ¶

2 năm trước

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:6

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

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:8

set_time_limit(20)6

Robertbrogers tại Gmail Dot Com ¶

PHP tại Mightycpa.com

my_exec.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);
         }
0

my_exec.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);
         }
1

my_exec.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);
         }
2

my_exec.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);
         }
3

set_time_limit(20)6

19 năm trước

f.nakamura ¶

my_exec.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);
         }
5

my_exec.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);
         }
6

my_exec.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);
         }
7

my_exec.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);
         }
8

set_time_limit(20)6

7 năm trước

15 năm trước

function 0

function 1

function 2

function 3

function 4

function 5

function 6

function 7

function 8

set_time_limit(20)6

Jonathon Dot Keogh tại Gmail Dot Com ¶

f.nakamura ¶

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 pgidstream_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:
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:
require 'my_exec.php';my_background_exec('A::jack', array('hello', 'jack'), 'require "my_exec.php";require "a_class.php";', 8);
?>

0

7 năm trước

2 năm trước

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 pgidstream_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:
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:
require 'my_exec.php';my_background_exec('A::jack', array('hello', 'jack'), 'require "my_exec.php";require "a_class.php";', 8);
?>

1

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 pgidstream_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:
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:
require 'my_exec.php';my_background_exec('A::jack', array('hello', 'jack'), 'require "my_exec.php";require "a_class.php";', 8);
?>

2

set_time_limit(20)6

Robertbrogers tại Gmail Dot Com ¶

PHP tại Mightycpa.com

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 pgidstream_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:
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:
require 'my_exec.php';my_background_exec('A::jack', array('hello', 'jack'), 'require "my_exec.php";require "a_class.php";', 8);
?>

4

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 pgidstream_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:
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:
require 'my_exec.php';my_background_exec('A::jack', array('hello', 'jack'), 'require "my_exec.php";require "a_class.php";', 8);
?>

5

set_time_limit(20)6

19 năm trước

PHP tại Mightycpa.com

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 pgidstream_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:
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:
require 'my_exec.php';my_background_exec('A::jack', array('hello', 'jack'), 'require "my_exec.php";require "a_class.php";', 8);
?>

7

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 pgidstream_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:
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:
require 'my_exec.php';my_background_exec('A::jack', array('hello', 'jack'), 'require "my_exec.php";require "a_class.php";', 8);
?>

8

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 pgidstream_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:
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:
require 'my_exec.php';my_background_exec('A::jack', array('hello', 'jack'), 'require "my_exec.php";require "a_class.php";', 8);
?>

9

while setting the set_time_limit(), the duration of sleep() will be ignored in the execution time. The following illustrates:0

Giới hạn thời gian PHP là gì?

Theo mặc định, thời gian thực hiện tối đa cho các tập lệnh PHP được đặt thành 30 giây.Nếu một tập lệnh chạy dài hơn 30 giây, PHP sẽ dừng tập lệnh và báo cáo lỗi.Bạn có thể kiểm soát lượng thời gian PHP cho phép các tập lệnh chạy bằng cách thay đổi Chỉ thị MAX_EXECUTY_TIME trong PHP của bạn.Tệp INI.30 seconds. If a script runs for longer than 30 seconds, PHP stops the script and reports an error. You can control the amount of time PHP allows scripts to run by changing the max_execution_time directive in your php. ini file.

PHP CLI có thời gian chờ không?

Nếu nhị phân PHP của bạn là giao diện PHP CLI, thì max_execut_time mặc định bằng 0 (có nghĩa là không có giới hạn).the default max_execution_time is zero (meaning there is no limit).

Chúng ta có thể đặt thời gian thực thi vô hạn trong PHP không?

Có, có thể đặt thời gian thực thi vô hạn cho tập lệnh PHP.Chúng ta có thể làm điều đó bằng cách thêm hàm set_time_limit () ở đầu tập lệnh PHP.. We can do it by adding the set_time_limit() function at the beginning of the PHP script.

Thời gian thực hiện mặc định cho các tập lệnh PHP là gì?

Thời gian thực hiện tối đa mặc định của tập lệnh PHP là 30 giây.Vì vậy, nếu việc chạy tập lệnh vượt quá 30 giây thì hệ thống sẽ gây ra lỗi.30 seconds. So if the running of the script exceeds 30 seconds then the system will throw an error.