• 技术文章 >后端开发 >php教程

    php的set_time_out和max_execution_time设置

    不言不言2018-04-26 14:27:53原创1992
    本片文章向大家介绍了关于php的set_time_out和max_execution_time设置,有需要的朋友可以参考一下。


    本意


    想让一个php脚本(fpm或者cli下)通过set_time_out或者max_execution_time设置只执行5秒。

    我原想是这样的代码

    <?phpini_set("max_execution_time",5);
    set_time_limit(5);for($i=0;$i<=10;$i++)
    {echo $i."\n";
    sleep(1);
    }

    但是cli下结果是

    012345678910

    fpm也一样。

    思考之路

    首先看手册函数啊,摘超如下:

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

    仍然是一脸懵逼。
    另外看到另外一句:

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

    特地看了一眼,不是安全模式啊。。

    然后百思不得,得到了大官人的耐心解答。

    php zend引擎实现max_execute_time是使用的settimer,参数是ITIMER_PROF(也就是说这只计算用户态和内核态使用的真正消耗的时间)
    但是sleep是挂起进程一段时间,并没有执行操作,也不存在消耗时间,所以这个sleep既不消耗内核态时间也不消耗用户态时间。
    写了段C程序验证了一下,确实在sleep状态下,根本没有时间统计,也不会触发signal handler..

    那我追一下这个[ITIMER_PRO][3]F参数

    Description
    The system provides each process with three interval timers, each decrementing in a distinct time domain. When any timer expires, a signal is sent to the process, and the timer (potentially) restarts.

    ITIMER_REAL
    decrements in real time, and delivers SIGALRM upon expiration.

    ITIMER_VIRTUAL

    decrements only when the process is executing, and delivers SIGVTALRM upon expiration.

    ITIMER_PROF

    decrements both when the process executes and when the system is executing on behalf of the process. Coupled with ITIMER_VIRTUAL, this timer is usually used to profile the time spent by the application in user and kernel space. SIGPROF is delivered upon expiration.

    看ITIMER_PROF选项中process executes 不就是php进程执行吗?是进程执行没问题,但是sleep函数会将进程挂起,所以sleep内的不算了。所以,在用户态执行的时间,是除开你sleep后的所有时间

    果真有具体差别,那么源码中中具体怎样体现的?再追一下

    main.c下追到以下

    /* {{{ proto bool set_time_limit(int seconds)
       Sets the maximum time a script can run */PHP_FUNCTION(set_time_limit)
    {
        zend_long new_timeout;
        char *new_timeout_str;
        int new_timeout_strlen;
        zend_string *key;    if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &new_timeout) == FAILURE) {        return;
        }    new_timeout_strlen = (int)zend_spprintf(&new_timeout_str, 0, ZEND_LONG_FMT, new_timeout);
    
        key = zend_string_init("max_execution_time", sizeof("max_execution_time")-1, 0);    if (zend_alter_ini_entry_chars_ex(key, new_timeout_str, new_timeout_strlen, PHP_INI_USER, PHP_INI_STAGE_RUNTIME, 0) == SUCCESS) {
            RETVAL_TRUE;
        } else {
            RETVAL_FALSE;
        }
        zend_string_release(key);
        efree(new_timeout_str);
    }

    我们看key那一行的sizeof("max_execution_time")
    然后追一下max_execution_time
    还是再main.c下,有

            }        if (PG(max_input_time) != -1) {#ifdef PHP_WIN32
                zend_unset_timeout();#endif
                zend_set_timeout(INI_INT("max_execution_time"), 0);
            }

    然后再zend目录下搜索zend_set_timeout,然后再zend_execute_api.c中找到ITIMER_PROF


    就是他了!

    以上就是php的set_time_out和max_execution_time设置的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:execution CSDN time
    上一篇:PHP GD库添加freetype拓展的方法 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • PHP Hyperf 3.0 发布!新功能速览• 详解PHP怎么实现旋转图片验证• 简单理解PHP超级全局变量• 一起聊聊PHP的路由与伪静态应用• PHP中几种常见的开发模式
    1/1

    PHP中文网