zoukankan      html  css  js  c++  java
  • php页面最大执行时间 set_time_limit函数不起作用

     
    作者: default|标签:PHP set_time_limit 执行时间|2017-3-21 15:03
     
    set_time_limit 不生效或者无效解决方法
    <?php
    global $begin;
    global $end;
    $begin = microtime(TRUE);
    try {
    $dsn = "mysql:host=localhost;dbname=kaixin";
    $db = new PDO($dsn, 'root', '111');
    $db->exec("set names 'utf8'");//默认编码
    $db->setAttribute(PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
    for($i=1; $i<5000; $i++){
    $sql = "insert into `article` (`title`, `content`, `time`, `author`, `ip`) value ('文章标题".$i."', '内容".$i."',
    '1227237892','kaixin','127.0.0.1')";
    $db->exec($sql);
    }
    $end = microtime(TRUE);
    // by bbs.it-home.org
    $time = $end-$begin;
    echo "执行了".$time."s";
    } catch (Exception $e) {
    echo "Failed: " . $e->getMessage();
    }
    ?>
    
     

    执行这段代码,只能插入大概1000条左右的数据,这样页面就不执行了。 测试页面的执行时间大概都是29秒左右,也就是页面的最大执行时间 是30秒。

    知道set_time_limit函数,可以设置页面执行时间。

    set_time_limit函数用法如下:

    本函式用来设定该页最久执行时间。内定值是 30 秒,在 php.ini 中的 max_execution_time 变数设定,若设定为 0 则不限定最久时间。当执行到该函数时,才开

    始计算。例如,若内定是 30 秒,而在执行到该函数前已执行了 25 秒,而用本函式改为 20 秒,则该页面最长执行时间为 45 秒。

    把这个set_time_limit函数加到了程序里,

    发现页面执行时间还是29秒左右,没有起作用。

    看到网上有人说,php.ini里的safe_mode如果为on,本函数不执行。查了我的php.in里的safe_mode=off。

    试着把set_time_limit(0);加入到for循环里。

    <?php
    ...
    for($i=1; $i<5000; $i++){
    set_time_limit(0);
    $sql = "insert into `article` (`title`, `content`, `time`, `author`, `ip`) value ('文章标题".$i."', '内容".$i."',
    <?php
    set_time_limit(0);
    global $begin;
    global $end;
    $begin = microtime(TRUE);
    ...
    ?>
    '1227237892','kaixin','127.0.0.1')";
    $db->exec($sql);
    }
    ...
    ?>
    
     

    也没有起作用。最后在网上查到,有人说,“set_time_limit函数最好是在linux下执行,windows执行可能也无效”。彻底对这个函数失去信心 了,估计是因为我是windows系统的原因。

    只能修改php.ini里的max_execution_time = 30了。这个默认是30秒,我修改为max_execution_time = 300.重新启动apache服务器。 结果执行时间为140秒左右,5000条数据终于插入了。 看来,windows下这个页面执行时间还是在php.ini里控制,修改max_execution_time。 如果用PDO往MYSQL里面插入数据的话。插入五千条大概140多秒时间,插入1万条大概260多秒时间。

    以上就是php页面最大执行时间 set_time_limit函数不起作用 的内容,更多相关内容请关注PHP中文网

  • 相关阅读:
    oracle触发器
    oracle存储函数
    ****Java程序调用存储过程****
    oracle储存过程--存储过程
    oracle储存过程--游标
    oracle存储过程--流程控制(条件判断和循环遍历)
    InterviewProblems
    RandomAccessFile浅析
    JSP基础——关于中文乱码问题
    基础数据结构——单链表
  • 原文地址:https://www.cnblogs.com/lxwphp/p/8808790.html
Copyright © 2011-2022 走看看