限制函数或命令PHP的执行时间 [英] Limit execution time of an function or command PHP

查看:280
本文介绍了限制函数或命令PHP的执行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能只设置命令或功能的时间限制,例如:

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的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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