问题描述
是否可以仅对命令或仅对函数设置时间限制,例如:
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..
}
我只想为function1设置时间限制.
I want to set time limit only to function1.
有退出 set_time_limit 但我认为这设置了整个脚本的时间限制.有人有什么想法吗?
There exits set_time_limit but I think this sets the time limit to whole script. Anybody any Idea?
推荐答案
set_time_limit() 确实在全局运行,但可以在本地重置.
set_time_limit() does run globally, but it can be reset locally.
设置允许脚本运行的秒数.如果达到了,该脚本返回一个致命错误.默认限制为 30 秒,或者,如果存在,php.ini 中定义的 max_execution_time 值.
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.
调用时,set_time_limit(
) 从零重新启动超时计数器.在换句话说,如果超时是默认的 30 秒,和 25 秒进入脚本执行调用如 set_time_limit(20) 时,脚本将在超时前总共运行 45 秒.
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.
我没有测试过,但你可以在本地设置,离开时重置
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
()... 在确定脚本运行的最长时间时,不包括在脚本执行之外发生的活动上花费的任何时间,例如使用 system() 的系统调用、流操作、数据库查询等.在测量时间是真实的 Windows 上,情况并非如此.
这篇关于限制函数或命令 PHP 的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!