Linux 上 PHP 的真正 max_execution_time [英] Real max_execution_time for PHP on linux

查看:27
本文介绍了Linux 上 PHP 的真正 max_execution_time的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文档:

max_execution_time only affect the execution time of the script itself. 
Any time spent on activity that happens outside the execution of the script
such as system calls using system(), stream operations, database queries, etc.
is not included when determining the maximum time that the script has been running.
This is not true on Windows where the measured time is real.

这是通过测试证实的:

不会超时

<?php
set_time_limit(5);
$sql = mysqli_connect('localhost','root','root','mysql');
$query = "SELECT SLEEP(10) FROM mysql.user;";
$sql->query($query) or die($query.'<br />'.$sql->error);
echo "You got the page";

将超时

<?php
set_time_limit(5);
while (true) {
  // do nothing
}
echo "You got the page";

我们的问题是我们真的希望 PHP 在给定的时间后超时,不管它在做什么(因为我们不想让资源忙,如果我们知道我们在可接受的时间量,如 10 秒).我们知道我们可以使用诸如 MySQL 之类的设置wait_timeout 用于 SQL 查询,但页面超时将取决于执行的查询数量.

Our problem is that we really would like PHP to timeout, regardless of what it is doing, after a given amount of time (as we don't want to keep resources busy if we know we've failed delivering a page in an acceptable amount of time, like 10 seconds). We know we can play with settings such as the MySQL wait_timeout for the SQL queries, but the page timeout will depend on the number of queries that are executed.

有些人试图想出解决方法 而且似乎无法实现.

Some people have tried to come up with workarounds and it doesn't seem implementable.

问:有没有一种简单的方法可以在 linux 上获得真正的 PHP max_execution_time,还是我们在其他地方更好地超时,例如 Apache 级别?

Q: Is there an easy way to get a real PHP max_execution_time on linux, or are we better timing out elsewhere, such as Apache level?

推荐答案

这是一个相当棘手的建议,但如果您愿意修改和重新编译 PHP,它肯定会满足您的需求.

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

https://github.com/上查看 PHP 源代码php/php-src/blob/master/Zend/zend_execute_API.c(文件是Zend/zend_execute_API.c),函数zend_set_timeout.这是实现时间限制的功能.以下是它在不同平台上的工作方式:

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:

  • 在 Windows 上,创建一个新线程,在它上面启动一个计时器,当它完成时,将一个名为 timed_out 的全局变量设置为 1,PHP 执行核心检查这个变量的每个指令,然后退出(非常简化)

  • 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)

在 Cygwin 上,将定时器与 ITIMER_REAL 一起使用,它测量实时时间,包括任何睡眠、等待等,然后发出一个 信号,将中断 >任何处理和停止处理

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

在其他 unix 系统上,将 itimer 与 ITIMER_PROF 一起使用,它仅测量当前进程(但在用户模式和内核模式下)花费的 CPU 时间.这意味着等待其他进程(如 MySQL)不算在内.

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.

现在你要做的就是把你Linux上的timer从ITIMER_PROF改成ITIMER_REAL,当然你需要手动做,重新编译,安装等.这两者的另一个区别是它们在什么时候也使用不同的信号计时器用完.所以我的建议是改变 ifdef:

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__

进入

#   if 1

以便您将 ITIMER_REAL 和 PHP 等待的信号设置为 SIGALRM.

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

无论如何,这整个想法未经测试(我将它用于某些非常具体的系统,其中 ITIMER_PROF 已损坏,并且它似乎可以工作)、不受支持等.使用它需要您自担风险.它可能适用于 PHP 本身,但它可能会破坏 PHP 和 Apache 中的其他模块,如果它们出于某种原因使用 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.

这篇关于Linux 上 PHP 的真正 max_execution_time的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆