限制函数或命令 PHP 的执行时间

Limit execution time of an function or command PHP(限制函数或命令 PHP 的执行时间)
本文介绍了限制函数或命令 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 的执行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)