php 设置 max_execution_time,php的set_time_out和max_execution_time设置

本片文章向大家介绍了关于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

e5182f043bada2f07e121e8130d2174d.png

就是他了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值