php.ini配置max_execution_time和FPM配置request_terminate_timeout

来源:互联网 发布:星际公民 知乎 编辑:程序博客网 时间:2024/05/21 06:28

PHP限定脚本执行时长的方式有几种,下面说下php.ini中的max_execution_time和php-fpm.conf中的request_terminate_timeout

1. php.ini中的max_execution_time

; Maximum execution time of each script, in seconds; http://php.net/max-execution-time; Note: This directive is hardcoded to 0 for the CLI SAPImax_execution_time = 1

上面是php.ini配置文件中的max_execution_time及其说明,上面说了,这个值限定了脚本的最大执行时间(单位是秒)

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

2. php-fpm.conf中的request_terminate_timeout

; The timeout for serving a single request after which the worker process will; be killed. This option should be used when the 'max_execution_time' ini option; does not stop script execution for some reason. A value of '0' means 'off'.; Available units: s(econds)(default), m(inutes), h(ours), or d(ays); Default Value: 0;request_terminate_timeout = 0request_terminate_timeout = 4s

设置单个请求的超时中止时间。该选项可能会对 php.ini 设置中的 max_execution_time 因为某些特殊原因没有中止运行的脚本有用。设置为 ‘0’ 表示 ‘Off’。可用单位:s(秒),m(分),h(小时)或者 d(天)。默认单位:s(秒)。默认值:0(关闭)。

通过上面两个说明及实验验证得出结论,max_execution_time=1,不一定1s后就会中止脚本,可能是2s、3s甚至更长的时间;而request_terminate_timeout=4则就会在4s后中止脚本的执行。所以在配置超时时间的时候,最好两个都配置,max_execution_time时间短一点,而request_terminate_timeout时间长一点

1 0
原创粉丝点击