PHP script running timeout management mechanism

Source: Internet
Author: User
In our normal development, we may have encountered a PHP script running timeout. in this case, PHP will display an error saying "Fatalerror: MaximumexecutiontimeofXXXsecondsexceededinXXX" and terminate the script running. In this case, we often use set_time_limit (unsecure mode)

During our normal development, we may have encountered a PHP script running timeout. at this time, PHP will display an error saying: "Fatal error: maximum execution time of XXX seconds exceeded in XXX "and stops the script. In this case, we often use set_time_limit (non-secure mode), modify the configuration file and restart the server, or modify the program to reduce the program execution time so that it is within the permitted range, to solve this problem. However, these are the representations we can see at the application layer. in the PHP kernel, there is a set of such mechanisms to support this representation.

This is a timeout management mechanism for PHP to prevent the execution of some business scripts from blocking the processing of other scripts or consuming server resources for a long time. In essence, PHP implements a timer for different platforms, manages and controls the timer running by relying on the global variable timeout (EG (timeout_seconds) during runtime. All management of the running duration of the script, including the configuration of interface functions and configuration files for the maximum running duration, is ultimately achieved by managing the timeout global variables and restarting the timer.


Initialization and timeout configuration items

In the/main. c file of the PHP kernel, the core configuration items of PHP and the on_modify method corresponding to each configuration item are defined. During module initialization (php_module_startup), the PHP kernel will call the ini configuration registration function and add the defined core configuration items to the ini configuration instruction set, the on_modify method corresponding to each configuration item is called.

The max_execution_time configuration item used to define the maximum running time of the script is also a member of these core configuration items. its default value is 30 seconds, and the corresponding on_modify method is OnUpdateTimeout. When these core configuration items are registered, the on_modify method of max_execution_time is called. The value of the configuration item is passed to the timeout global variable EG (timeout_seconds), and the timer is started using the zend_set_timeout method.

For Windows and unix-like platforms, the PHP kernel implements different timers. On the Win32 Platform, a timer is encapsulated on the basis of WM_TIME. Create an independent thread to control the timer and create a message Ring. WaitForSingleObject is used to block the returned result of zend_init_timeout_thread. Start timing when WM_REGISTER_ZEND_TIMEOUT is received. In fact, the timing task is SetTimer (timeout_window, wParam, lParam * 1000, NULL). The system will send a WM_TIMER after seconds * 1000, at this time, the timer is ended and can be interrupted by WM_UNREGISTER_ZEND_TIMEOUT in the middle.

The unix-like platform uses the Linux API function setitimer to specify the SIGPROF signal as the timeout processing signal, corresponding to the timeout processing function zend_timeout. when a timeout occurs, this signal is sent and the zend_timeout function is triggered to display the error message and stop the program.

If you need to cancel the timer, Win platform sends WM_UNREGISTER_ZEND_TIMEOUT to the thread through PostThreadMessage. unix-like platforms reset the timer length to 0.


Timeout management

The timeout mechanism is very flexible, and there are three ways to modify the running duration.

1. modify configuration items. By default, the maximum runtime length of the PHP script is 30 s. To adjust this option, you can modify the maximum running duration by modifying the max_execution_time option in the php. ini file and restarting the server. This method applies to the initial default configuration modification, or when other methods are invalid.

2. use the set_time_limit interface function. This function is used to set the maximum execution time of the script. When this function is called, set_time_limit () restarts the timeout counter from scratch. For example, if the interval is set to 5 seconds and the interval is set to 5 seconds after the script runs for 4 seconds, the total running time of the script before the timeout is 10 seconds. Example script:

    
 "; }   set_time_limit(5);   for ($i = 0; $i < 4; $i++) { sleep(1); echo $i, "
"; }

In the above code, the program will execute two cycles and output 0, 1, 2, and 3. If we comment out the set_time_limit (5) in the middle and the program runs again, an error is reported after the second loop outputs 0.

In security mode, you cannot reset max_execution_time through set_time_limit and ini_set. you can modify this parameter only when you disable security mode or change the time limit in php. ini.

3. use ini_set to modify the max_execution_time parameter.

The implementation process of the above three methods is similar. the previous method is to call the on_modify pointer function during initialization. After processing the parameters, call the zend_alter_ini_entry_ex function to trigger the on_modify function. Therefore, all operations that manage the timeout mechanism are eventually integrated into the OnUpdateTimeout function. In this function, the script timeout value is reset through zend_set_timeout.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.