现在的位置: 首页 > 综合 > 正文

PHP中的set_time_limit,max_execution_time,sleep

2013年08月17日 ⁄ 综合 ⁄ 共 2043字 ⁄ 字号 评论关闭

手册中的说明:

set_time_limit()

set_time_limit — 设置脚本最大执行时间

说明

void set_time_limit ( int $seconds )

设置允许脚本运行的时间,单位为秒。如果超过了此设置,脚本返回一个致命的错误。默认值为30秒,或者是在php.inimax_execution_time被定义的值,如果此值存在。

当此函数被调用时,set_time_limit()会从零开始重新启动超时计数器。换句话说,如果超时默认是30秒,同时,脚本中又设置了25秒,如 set_time_limit(20)。那么,脚本在超时之前可运行总共时间为45秒。

参数

seconds

最大的执行时间,单位为秒。如果设置为0(零),没有时间方面的限制。

返回值

没有返回值。

注释

Warning

当php运行于安全模式下时,此功能不能生效。除了关闭安全模式或改变php.ini中的时间限制,没有别的办法。

Note:

set_time_limit()函数和配置指令max_execution_time只影响脚本本身执行的时间。任何发生在诸如使用system()的系统调用,流操作,数据库操作等的脚本执行的最大时间不包括其中,当该脚本已运行。在测量时间是实值的Windows中,情况就不是如此了。

安全模式下 set_time_limit()是不起作用的

max_execution_time

This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. When running PHP from the command
line
 the default setting is 0.

The maximum execution time is not affected by system calls, stream operations etc. Please see the set_time_limit() function
for more details.

You can not change this setting with ini_set() when
running in 安全模式. The only
workaround is to turn off safe mode or by changing the time limit in thephp.ini.

Your web server can have other timeout configurations that may also interrupt PHP execution. Apache has a Timeout directive and IIS has a CGI timeout function. Both default to 300 seconds. See your web server documentation for specific
details.

sleep()

sleep — Delay execution

说明

int sleep ( int $seconds )

Delays the program execution for the given number of seconds.

参数

seconds

Halt time in seconds.

返回值

Returns zero on success, or FALSE on error.

If the call was interrupted by a signal, sleep() returns a non-zero value. On Windows, this value will always be 192 (the value of theWAIT_IO_COMPLETION constant
within the Windows API). On other platforms, the return value will be the number of seconds left to sleep.

错误/异常

If the specified number of seconds is negative, this function will generate a E_WARNING.

更新日志

版本 说明
5.3.4 Before PHP 5.3.4, on Windows, sleep() always returns NULL when sleep
has occurred, regardless of whether the sleep was interrupted or not.

就是说 set_time_limit()函数 仅计算脚本自己执行的时间,调用system(),io操作,数据库查询,sleep() 函数不计算在脚本执行时间之内.

set_time_limit(20);  
for($i=0;$i<1000;$i++)                                                             
{  
      echo "i=$i\n";  
      sleep(5);  
}  


抱歉!评论已关闭.