php set_time_limit()用法测试

一、总结

一句话总结:在php中set_time_limit函数是用来限制页面执行时间的,如我想把一个php页面的执行时间定义为5秒就可以set_time_limit(5)了,规定从该句运行时起程序必须在指定秒数内运行结束,

限制页面执行时间

1、系统sleep()的时间会计算在set_time_limit()中么?

不会

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

2、想要不限制脚本的运行时间,set_time_limit()的函数参数应该是什么?

0

set_time_limit(0)表示长时间链接运行,不限制运行时间

3、set_time_limit()和php.ini里的max_execution_time的关系是什么?

最大执行时间=php.ini里的max_execution_time数值-当前脚本已经执行的时间+set_time_limit()设定值

最大执行时间=php.ini里的max_execution_time数值 - 当前脚本已经执行的时间 + set_time_limit()设定值
假如php.ini里的max_execution_time=30,当前脚本已经执行10秒,则:
最大执行时间=30-10+900=920秒。

二、php set_time_limit()用法测试详解

参考:php set_time_limit()用法测试详解 - everest33 - 博客园
https://www.cnblogs.com/everest33Tong/p/6177938.html

在php中set_time_limit函数是用来限制页面执行时间的,如我想把一个php页面的执行时间定义为5秒就可以set_time_limit(5)了。
 

一个php脚本通过crontab每5分钟执行一次,考虑到脚本执行时间会超过5分钟,特意用set_time_limit(290)来控制脚本在290秒退出。某天突然发现后台有多个该脚本的进程在执行,也就是说set_time_limit(290)没有起作用。为了证明,特意使用如下代码测试。

 代码如下 复制代码

set_time_limit(5);

for ($i = 0; $i < 100; $i++) {
    echo date('Y-m-d H:i:s') . "n";
    sleep(1);
}

无论是在web还是CLI下,上述脚本并没有在5秒钟后退出。后来加上ini_set(‘max_execution_time’, 5)测试,结果一样。那是不是说明set_time_limit函数根本就没有用呢?其实不然,在 http://stackoverflow.com/questions/5874950/set-max-execution-time-in-php-cli 这里找到根源所在,其实是上面的写法有问题,例如使用下述代码:

 代码如下 复制代码

set_time_limit(5);

for (;;) {
}

执行后,大概5秒钟就可以看到”Fatal error: Maximum execution time of 5 seconds exceeded in”类似这样的错误提示。说明set_time_limit是起作用的。现在在去看看官方文档(http://www.php.net/manual/en/function.set-time-limit.php)上关于此函数的说明,在Note中写到:

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

 代码如下 复制代码
<?php 
//set_time_limit(0); 
$i=1500; 
include ("inc/conn.php"); 
while($i>0) 

$sql="INSERT INTO php (php) 
VALUES ('$i')"; 
if ($conn->execute($sql)===flase) 

//echo "数据插入错误".$conn->errormsg(); 

else 

$phpid=$conn->Insert_ID(); 
echo $i."已经存入数据库,编号:".$phpid; 

$i--; 
echo "<hr>"; 

?>

注意:sleep函数暂停的时间也是不计入脚本的执行时间的。所以也是第一个测试失败的原因。

当你的页面有大量数据时,建议使用set_time_limit()来控制运行时间,默认是30s,所以需要你将执行时间加长点,如 set_time_limit(300)  ,其中将秒数设为0 ,表示持续运行!

如:set_time_limit(0)表示长时间链接运行!

注意:这个函数的运行需要你关闭安全模式,在php.ini中将safe_mode = Off 安全模式设置为Off,否则将会出现下面错误:

Warning: set_time_limit() [function.set-time-limit]: Cannot set time limit in safe mode in

再次注意的是:

在php.ini可以通过定义max_execution_time来设置PHP页面的最大执行时间,比如下面:

 代码如下 复制代码
set_time_limit(900);

这个函数指定了当前所在php脚本的最大执行时间,
虽然设定值是900秒,实际上
最大执行时间=php.ini里的max_execution_time数值 - 当前脚本已经执行的时间 + 设定值
假如php.ini里的max_execution_time=30,当前脚本已经执行10秒,则:
最大执行时间=30-10+900=920秒。

php中设置set_time_limit不起作用的解决方法:

set_time_limit用来设置脚本的超时时间,用法如下:

set_time_limit(秒数); 
规定从该句运行时起程序必须在指定秒数内运行结束, 
超时则程序出错退出. 
但是有时候设置set_time_limit没有效果,set_time_limit函数最好是在linux下执行,windows执行可能也无效 
解决方法: 
修改php.ini里的max_execution_time = 30了。这个默认是30秒,修改为max_execution_time = 300.重新启动apache服务器。这样超时设置为300秒就有提示信息了.

 

php set_time_limit()的作用是什么的更多相关文章

  1. php中(包括织梦cms)set_time_limit(0)不起作用的解决方法

    背景介绍: 在做织梦冗余图片清理的功能时, 由于冗余图片太多,导致每次清理时都会超时, 后来在网上搜索了各种文章,网上有如下的解决方法: set_time_limit(0) ini_set('max_ ...

  2. php页面最大执行时间 set_time_limit函数不起作用

      作者: default|标签:PHP set_time_limit 执行时间|2017-3-21 15:03   set_time_limit 不生效或者无效解决方法 <?php globa ...

  3. php set_time_limit()用法测试详解

    在php中set_time_limit函数是用来限制页面执行时间的,如我想把一个php页面的执行时间定义为5秒就可以set_time_limit(5)了.   一个php脚本通过crontab每5分钟 ...

  4. if __name__== "__main__" 的意思(作用)python代码复用

    if __name__== "__main__" 的意思(作用)python代码复用 转自:大步's Blog  http://www.dabu.info/if-__-name__ ...

  5. (转载)linux下各个文件夹的作用

    linux下的文件结构,看看每个文件夹都是干吗用的/bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基 ...

  6. github中的watch、star、fork的作用

    [转自:http://www.jianshu.com/p/6c366b53ea41] 在每个 github 项目的右上角,都有三个按钮,分别是 watch.star.fork,但是有些刚开始使用 gi ...

  7. web.xml中welcome-file-list的作用

    今天尝试使用struts2+ urlrewrite+sitemesh部署项目,结果发现welcome-file-list中定义的欢迎页不起作用: <welcome-file-list> & ...

  8. web.xml中load-on-startup的作用

    如下一段配置,熟悉DWR的再熟悉不过了:<servlet>   <servlet-name>dwr-invoker</servlet-name>   <ser ...

  9. SQLSERVER中NULL位图的作用

    SQLSERVER中NULL位图的作用 首先感谢宋沄剑提供的文章和sqlskill网站:www.sqlskills.com,看下面文章之前请先看一下下面两篇文章 SQL Server误区30日谈-Da ...

随机推荐

  1. 【题解】Luogu P2146 [NOI2015]软件包管理器

    题面:https://www.luogu.org/problemnew/lists?name=2146 这道题要用树链剖分,我博客里有对树链剖分的详细介绍 这道题就是树链剖分的模板,详细解释见程序. ...

  2. 线程同步——用户模式下线程同步——Slim读写锁实现线程同步

    //Slim读/写锁实现线程同步 SRWlock 的目的和关键段相同:对同一资源进行保护,不让其它线程访问. 但是,与关键段不同的是,SRWlock允许我们区分哪些想要读取资源的线程(读取者线程) 和 ...

  3. 搭建ldap服务器及web管理服务--phpldapadmin

    系统版本:centos6 安装配置openldap: yum install openldap openldap-servers openldap-clients openldap-devel com ...

  4. 缩点:Power Plant;

    题目传送门:[UVALive 6437]Power Plant 题目大意:T组数据,给定一幅带权图(n, m), 然后给定k个点, 与图中存在有若干条边.每个点都要至少要和这k个点的一个点直接或间接相 ...

  5. 快速阅读《QT5.9 c++开发指南》1

    简介:<QT5.9 c++开发指南>的作者是和i三位主要从事地球物理探测仪器设计.数据处理方法研究和软件开发等工作的博士们,这本书以QT Widget为主要内容,比较全面地教授了QT开发桌 ...

  6. BSGS算法学习笔记

    从这里开始 离散对数和BSGS算法 扩展BSGS算法 离散对数和BSGS算法 设$x$是最小的非负整数使得$a^{x}\equiv b\ \ \ \pmod{m}$,则$x$是$b$以$a$为底的离散 ...

  7. topcoder srm 694 div1 -3

    1.给出$n$个数字,将其分成三个非空的组,每组的权值为该组所有数字的抑或.选择一种分法使得三组的权值和最大? 思路:记录前两组的权值且三组有没有数字时第三组的值.(当前两组的值知道时第三组的权值是确 ...

  8. SpringBoot 读取properties配置文件 @Value使用 中文乱码问题

    一,idea中配置文件中文乱码问题 使用idea开发,读取properites配置文件 配置: #app 菜单 #没有限制,所有人都可访问的菜单 menu.unlimited=订单审批,现场尽调,合作 ...

  9. Print a file's last modified date in Bash

    date -r <filename> #!/usr/bin/env bash for i in /var/log/*.out; do stat -f "%Sm" -t ...

  10. CF981D Bookshelves

    按位贪心+DP的好题qwq 首先看到题目的要求,统计价值的时候的操作是按位与,就要有按位分别计算的意识 开始没意识到结果想了好久还是看了题解才想到 由于统计价值的方式不是加和,所以可能会出现两个较大的 ...