Home  >  Article  >  Backend Development  >  PHP's set_time_out and max_execution_time settings

PHP's set_time_out and max_execution_time settings

不言
不言Original
2018-04-26 14:27:532981browse

This article introduces you to the set_time_out and max_execution_time settings of php. Friends in need can refer to it.


Intention


I want a php script (under fpm or cli) to only execute 5 through set_time_out or max_execution_time settings Second.

I originally thought it was this code

<?phpini_set("max_execution_time",5);
set_time_limit(5);for($i=0;$i<=10;$i++)
{echo $i."\n";
sleep(1);
}

But the result under cli is

012345678910

The same goes for fpm.

Thinking Path

First read the manual function, the summary is as follows:

The set_time_limit() function and the configuration instruction max_execution_time only affect the execution time of the script itself. The maximum time for any script execution that occurs such as system calls using system(), stream operations, database operations, etc. is not included when the script is already running. In Windows, where measured times are real-valued, this is not the case.

Still confused.
Also see another sentence:

When php is running in safe mode, this function cannot take effect. There is no other way except turning off the safe mode or changing the time limit in php.ini

I took a special look and it was not safe mode. .

Then I was puzzled and received a patient answer from the official.

php zend engine implements max_execute_time using a settimer, and the parameter is ITIMER_PROF (that is to say, this only calculates the real time consumed by user mode and kernel mode)
But sleep suspends the process for a period of time. There is no operation performed and no time consumed, so this sleep consumes neither kernel mode time nor user mode time.
I wrote a C program and verified it. Indeed, in the sleep state, there is no time statistics at all, and the signal handler will not be triggered..

Then I will chase this [ITIMER_PRO][3 ]FParameter

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. 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.

Look at the process executes in the ITIMER_PROF option. Isn’t it the php process execution? There is no problem with process execution, but the sleep function will suspend the process, so the content in sleep is not counted. Therefore, the execution time in user mode is all the time except after you sleep. If there is a specific difference, how is it reflected in the source code? Chase again

and then

main.c

to the following <pre class="hljs haxe">/* {{{ 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(), &quot;l&quot;, &amp;new_timeout) == FAILURE) { return; } new_timeout_strlen = (int)zend_spprintf(&amp;new_timeout_str, 0, ZEND_LONG_FMT, new_timeout); key = zend_string_init(&quot;max_execution_time&quot;, sizeof(&quot;max_execution_time&quot;)-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); }</pre> Let’s look at the key line

sizeof("max_execution_time")

Then chase max_execution_time or go to main.c, there is

        }        if (PG(max_input_time) != -1) {#ifdef PHP_WIN32
            zend_unset_timeout();#endif
            zend_set_timeout(INI_INT("max_execution_time"), 0);
        }

, then search zend_set_timeout in zend directory, and then find ITIMER_PROF

in zend_execute_api.c
It’s him!

The above is the detailed content of PHP's set_time_out and max_execution_time settings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn