php的set_time_out和max_execution_time设置
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php的set_time_out和max_execution_time设置,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3157字,纯文字阅读大概需要5分钟。
内容图文
![php的set_time_out和max_execution_time设置](/upload/InfoBanner/zyjiaocheng/139/e11ab31fb3494414a2d8594a263ab434.jpg)
本意
想让一个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设置的详细内容,更多请关注Gxl网其它相关文章!
内容总结
以上是互联网集市为您收集整理的php的set_time_out和max_execution_time设置全部内容,希望文章能够帮你解决php的set_time_out和max_execution_time设置所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。