Real max_execution_time for PHP on linux

前端 未结 3 1023
忘掉有多难
忘掉有多难 2020-12-19 12:03

According to the documentation:

max_execution_time only affect the execution time of the script itself. 
Any time spent on activity that happens outside the e         


        
相关标签:
3条回答
  • 2020-12-19 12:18

    This is quite a tricky advice, but it will definitely do what you want, if you are willing to modify and recompile PHP.

    Take a look at the PHP source code at https://github.com/php/php-src/blob/master/Zend/zend_execute_API.c (the file is Zend/zend_execute_API.c), at function zend_set_timeout. This is the function that implements time limit. Here's how it works on different platforms:

    • on Windows, create a new thread, start a timer on it, and when it finishes, set a global variable called timed_out to 1, the PHP execution core checks this variable for every instruction, then exits (very simplified)

    • on Cygwin, use itimer with ITIMER_REAL, which measures real time, including any sleep, wait, whatever, then raise a signal that will interrupt any processing and stop processing

    • on other unix systems, use itimer with ITIMER_PROF, which only measures CPU time spent by the current process (but both in user-mode and kernel-mode). This means waiting for other processes (like MySQL) doesn't count into this.

    Now what you want to do is to change the itimer on your Linux from ITIMER_PROF to ITIMER_REAL, which of course you need to do manually, recompile, install etc. The other difference between these two is that they also use different signal when the timer runs out. So my suggestion is to change the ifdef:

    #   ifdef __CYGWIN__
    

    into

    #   if 1
    

    so that you set both ITIMER_REAL and the signal that PHP waits for to SIGALRM.

    Anyway this whole idea is untested (I use it for some very specific system, where ITIMER_PROF is broken, and it seems to work), unsupported, etc. Use it at your own risk. It may work with PHP itself, but it could break other modules, in PHP and in Apache, if they for whatever reason, use the SIGALRM signal or other timer.

    0 讨论(0)
  • 2020-12-19 12:38

    This is an old and answered question. But for the sake of helping others, I wanted to point out the request_terminate_timeout php-fpm option. If you're using PHP-FPM, it is most likely what you need.

    If set, this option allows you to tell PHP-FPM to kill a request after N seconds, regardless of what PHP does.

    See http://php.net/manual/en/install.fpm.configuration.php#request-terminate-timeout for details.

    0 讨论(0)
  • 2020-12-19 12:39

    From httpd.conf:

    Timeout: The number of seconds before receives and sends time out

    Timeout 300

    0 讨论(0)
提交回复
热议问题