PHP:超过 30 秒的最大执行时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4051107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:49:41  来源:igfitidea点击:

PHP: Maximum execution time of 30 seconds exceeded

phpclass

提问by iamjonesy

I have a php app that calls a class called Client. Every so often I get a sort of time out error. I thought it was SQL at first but it turns its pointing to the class itself.

我有一个 php 应用程序,它调用一个名为 Client 的类。每隔一段时间,我就会收到某种超时错误。一开始我以为是 SQL,但它转而指向类本身。

Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\ClientPortal\classes\Connections.php on line 3

致命错误:第 3 行 C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\ClientPortal\classes\Connections.php 中超出了 30 秒的最大执行时间

<?php
    session_start();
    class Connections { //line 3

Does anyone know what's going on here?

有谁知道这里发生了什么?

thanks,

谢谢,

Billy

比利

回答by Shabbyrobe

PHP scripts have a maximum time they're allowed to execute for, as declared in the php.ini.

如 php.ini 中声明的那样,PHP 脚本有一个最长执行时间。

You can circumvent this if you really want by adding the following line:

如果你真的想要,你可以通过添加以下行来规避这一点:

ini_set('max_execution_time', 123456);

where 123456 is the number of seconds you want the limit to be.

其中 123456 是您希望限制的秒数。

You can also use the set_time_limitfunction, which I only just found out about and assume does the same thing. I've always just done the former though.

您还可以使用set_time_limit函数,我刚刚发现并假设它可以做同样的事情。不过我一直只做前者。

You can change it in the php.ini file, but you might be using your script to do a batch operation or something. You wouldn't want a PHP script that is being accessed by an end user to sit there hanging for 30 seconds or more though, so you're better off leaving it at the default or even turning it down in the php.ini file, and setting the max_execution_time on an as-needed basis.

您可以在 php.ini 文件中更改它,但您可能正在使用脚本来执行批处理操作或其他操作。您不希望最终用户正在访问的 PHP 脚本在那里挂起 30 秒或更长时间,因此您最好将其保留为默认值,甚至在 php.ini 文件中将其关闭,并根据需要设置 max_execution_time。

As seengee points out in the comment below, you can set the max_execution_time to 0 to stop the error from ever happening, but seengee is right to say that at least for a web request, you really shouldn't do this. For the php command line interpreter, this behaviour is the default though.

正如 seegee 在下面的评论中指出的那样,您可以将 max_execution_time 设置为 0 以阻止错误发生,但 seegee 说至少对于 Web 请求,您确实不应该这样做。对于 php 命令行解释器,这种行为是默认的。

If you're seeing this problem for things that aresupposed to be used by end-users through a web request, you might have to do some profiling to work out the real cause. If you're doing MySQL queries, start by turning on the slow query log. It's particularly good at letting you know when you've forgotten an index, or if you're doing something else inefficient.

如果您发现最终用户应该通过 Web 请求使用的东西存在此问题,可能需要进行一些分析才能找出真正的原因。如果您正在执行 MySQL 查询,请先打开慢查询日志。它特别擅长让您知道何时忘记了索引,或者您是否正在做其他效率低下的事情。

You can also shove a few $s = microtime(true); yourstuff(); var_dump(microtime(true)-$s);things around to get a vague overview of which bits are slowing things down, just make sure you don't leave any of them in afterwards!

您还可以$s = microtime(true); yourstuff(); var_dump(microtime(true)-$s);四处移动一些东西,以模糊地了解哪些部分正在减慢速度,只需确保之后不要留下任何东西!

If you're still struggling to find the root cause, set xdebugup on your local machine and run the profiler. The extension is available as a precompiled windows binary (although there seems to be a confusing array of versions). You can inspect the results of running the profiler using wincachegrind.

如果您仍在努力寻找根本原因,在本地计算机上设置xdebug并运行分析器。该扩展程序可作为预编译的 Windows 二进制文件使用(尽管似乎有一系列令人困惑的版本)。您可以使用wincachegrind检查运行分析器的结果。