限制函数或命令 PHP 的执行时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1176497/
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
Limit execution time of an function or command PHP
提问by Granit
Hi is there a possibility to set time limit only to a command or only to a function eg:
嗨,是否可以仅对命令或仅对函数设置时间限制,例如:
function doSomething()
{
//..code here..
function1();
//.. some code here..
}
I want to set time limit only to function1.
我只想将时间限制设置为 function1。
There exits set_time_limit but I think this sets the time limit to whole script. Anybody any Idea?
有退出 set_time_limit 但我认为这设置了整个脚本的时间限制。有人有什么想法吗?
回答by Alister Bulman
set_time_limit() does run globally, but it can be reset locally.
set_time_limit() 确实在全局运行,但可以在本地重置。
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.
When called,
set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.
设置允许脚本运行的秒数。如果达到,脚本将返回一个致命错误。默认限制为 30 秒,如果存在,则为 php.ini 中定义的 max_execution_time 值。
调用时,
set_time_limit() 从零重新启动超时计数器。换句话说,如果超时是默认的 30 秒,并且在脚本执行 25 秒后调用了诸如 set_time_limit(20) 之类的调用,则脚本将在超时前总共运行 45 秒。
I've not tested it, but you may be able to set it locally, resetting when you leave the
我没有测试过,但你可以在本地设置,离开时重置
<?php
set_time_limit(0); // global setting
function doStuff()
{
set_time_limit(10); // limit this function
// stuff
set_time_limit(10); // give ourselves another 10 seconds if we want
// stuff
set_time_limit(0); // the rest of the file can run forever
}
// ....
sleep(900);
// ....
doStuff(); // only has 10 secs to run
// ....
sleep(900);
// ....
set_time_limit()... 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.
set_time_limit()...在确定脚本运行的最长时间时,不包括在脚本执行之外发生的活动(例如使用 system() 的系统调用、流操作、数据库查询等)上花费的任何时间。在测量时间是真实的 Windows 上,情况并非如此。
回答by Paul Dixon
You would need to code that yourself if you want function1 to return when the time limit is up.
如果您希望 function1 在时间限制结束时返回,则需要自己编写代码。
function doStuff($seconds)
{
$finish=time()+$seconds;
$done=false;
$timeout=false;
while (!$done && !$timeout)
{
//do stuff here, set $done to true if you complete
//out of time?
$timeout=time() >= $finish;
}
return $done; //let caller know if we completed
}
Of course, the "stuff" that you do needs to be iterative so that you can keep checking on your elapsed time and quit if necessary.
当然,您所做的“东西”需要迭代,以便您可以继续检查已用时间并在必要时退出。
If you want timeout execution of an arbitrary function and have control returned to you, that is not supported. If you really need to do this, engineer it as a separate process. Then you can close the process if it seems to be taking too long.
如果您想超时执行任意函数并将控制权返回给您,则不支持。如果您确实需要这样做,请将其设计为一个单独的过程。然后,如果它看起来花费的时间太长,您可以关闭该过程。
回答by Pipe Soto
$endtime=time()+10;
while(time()!=$endtime){
echo ".";
}
回答by Pascal MARTIN
No, it is not possible : max_execution_timeis used as a security measure, to avoid having buggy scripts (like infinite loop) destroy the server ; but it is not intended to work on a function-unit basis.
不,这是不可能的:max_execution_time用作安全措施,以避免有错误的脚本(如无限循环)破坏服务器;但它并不打算在功能单元的基础上工作。
And the fact that it ends up with a Fatal Error is definitly not nice for the user anyway...
并且它以致命错误告终的事实无论如何对用户来说绝对不是好事......
回答by Pascal MARTIN
I use exec to run shellscript that generates export from external application. If script runs too long, max_execution_time is reached and user will never know what happened. Limitation to such function would help me determine if end is near and send some user friendly error.
我使用 exec 运行从外部应用程序生成导出的 shellscript。如果脚本运行时间过长,则会达到 max_execution_time 并且用户永远不会知道发生了什么。对此类功能的限制将帮助我确定 end 是否接近并发送一些用户友好的错误。
回答by Flo
i dont think that this is possible, but why would you do this? If a script ever gets interrupted i might not do want you want it to. This should be more like a protection from yourself (just in case you upload an endless loop or something that will take hours), but if your script takes lots of time, you should either give it the time that it takes, or change the script so that it runs faster (for some crons its ok to take a while, but if a normal script that is called quite often takes more then the default time limit, you might face some serious performance problems)
我不认为这是可能的,但你为什么要这样做?如果脚本被中断,我可能不希望您希望它中断。这应该更像是对自己的保护(以防万一您上传无限循环或需要数小时的内容),但是如果您的脚本需要很多时间,您应该给它所需的时间,或者更改脚本以便它运行得更快(对于某些 cron 需要一段时间,但是如果经常调用的普通脚本需要比默认时间限制更长的时间,您可能会面临一些严重的性能问题)

