php set_time_limit,确切的行为是什么?
php set_time_limit, what's the exact behavior?
我正在写一段 PHP 代码,在某个点 运行 是一个 foreach
循环,因为我担心它需要太长时间,所以我添加了语句 set_time_limit(20);
作为循环的第一行。 (我假设 safe_mode 和 Suhosin 补丁未启用。)
现在,我一直在 http://php.net/manual/en/function.set-time-limit.php 读到
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.
但我有点困惑。换句话说,如果循环运行s,比方说,10次,每次循环需要1秒到运行,在第10次循环结束时,max_execution_time
的值将是增加到 20 * (10 - 1) = 180 秒加上初始 max_execution_time
值?
每次使用 set_time_limit() 倒计时,直到脚本 killed/triggers max_execution_time 错误被重置。
在您的示例中,脚本可能会在最后一个循环后 运行 持续 20 秒。
引自此source:
The set_time_limit can be used to dynamically adjust the maximum execution time permitted to a script. It allows specifying the time in seconds and limits the script execution time to that many seconds.
The set_time_limit function whenever called, effectively extends the script execution time by that many seconds. So if the script has already run for 15 seconds and set_time_limit(30) is called, then it would run for a total of 30+15 = 45 seconds. That's how its designed to work.
我正在写一段 PHP 代码,在某个点 运行 是一个 foreach
循环,因为我担心它需要太长时间,所以我添加了语句 set_time_limit(20);
作为循环的第一行。 (我假设 safe_mode 和 Suhosin 补丁未启用。)
现在,我一直在 http://php.net/manual/en/function.set-time-limit.php 读到
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.
但我有点困惑。换句话说,如果循环运行s,比方说,10次,每次循环需要1秒到运行,在第10次循环结束时,max_execution_time
的值将是增加到 20 * (10 - 1) = 180 秒加上初始 max_execution_time
值?
每次使用 set_time_limit() 倒计时,直到脚本 killed/triggers max_execution_time 错误被重置。
在您的示例中,脚本可能会在最后一个循环后 运行 持续 20 秒。
引自此source:
The set_time_limit can be used to dynamically adjust the maximum execution time permitted to a script. It allows specifying the time in seconds and limits the script execution time to that many seconds.
The set_time_limit function whenever called, effectively extends the script execution time by that many seconds. So if the script has already run for 15 seconds and set_time_limit(30) is called, then it would run for a total of 30+15 = 45 seconds. That's how its designed to work.