欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

关于php的max_execution_time init_get init_set 有大用

关于php的max_execution_time

PHPor 的Blog  120 阅读

缘起:

一个php-fpm的请求执行时间会比较长,总是执行不完就中止了;查看php错误日志,显示执行时长超过了60s。

$ php -i |grep max_execution_time
$ max_execution_time => 0 => 0

为什么呢?

参考php官方文档: http://php.net/manual/en/info.configuration.php#ini.max-execution-time

max_execution_time integer

This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30 When running PHP from the command line the default setting is.

The maximum execution time is not affected by system calls, stream operations etc. Please see the set_time_limit() function for more details.

You can not change this setting with ini_set() when running in safe mode . The only workaround is to turn off safe mode or by changing the time limit in the php.ini .

Your web server can have other timeout configurations that may also interrupt PHP execution. Apache has a Timeout directive and IIS has a CGI timeout function. Both default to 300 seconds. See your web server documentation for specific details.

说明:

  1. 命令行执行php没有执行时长限制,尽管显式在php.ini中配置、或者ini_set(“max_execution_time”, n)、或者set_time_limit(n) 都是没用的;

  2. 尽管使用ini_set(…) 、set_time_limit(..) 后,也能通过ini_get(..)发现设置的值确实发生变化了,对于命令行也是没有作用的

  3. 命令行php -i 查看到的 max_execution_time 总是0

  4. 所以: 命令行程序对于PHP执行时长限制的担心都是多余的

  5. max_execution_time 的默认值是30s

关于set_time_limit: http://php.net/manual/en/function.set-time-limit.php

  1. set_time_limit(n) 会重置时间计数器,就是说是从该函数调用的时候计数n的

  2. php-fpm的脚本中执行set_time_limit(n)、或者ini_set(“max_execution_time”, n) 是无效的 
    虽然这样更加安全,但是不符合定义: http://php.net/manual/en/info.configuration.php ;这里 
    有说明max_execution_time的可修改限制为PHP_INI_ALL,就是说任何条件下都可以随时修改:http://php.net/manual/en/configuration.changes.modes.php

结论:

  1. 命令行程序对于PHP执行时长限制的担心都是多余的

  2. php-fpm环境下,试图在脚本中修改max_execution_time是没有用的

  3. 所以,max_execution_time 一般在php.ini 中定义


来自 http://ju.outofmemory.cn/entry/278406

普通分类: