php页面函数设置超时限制的方法_php技巧

WBOY
Release: 2016-05-16 20:29:20
Original
1113 people have browsed it

本文实例讲述了php页面函数设置超时限制的方法。分享给大家供大家参考。具体方法如下:

碰到页面程序执行超时时会提醒Fatal error: Maximum execution time of 300 seconds exceeded 是因为程序执行时间超过了最大允许执行时间,解决办法我们总结了几个供大家选择.

对于函数我们可以常用下面方法,直接给函数设置超时时间来操作,代码如下:

复制代码 代码如下:
declare(ticks = 1);
    function a(){
    sleep(10);
    echo "a finishi ";
}
function b(){
    echo "Stop ";
}
function c(){
    usleep(100000);
}
 
function sig(){
    throw new Exception;
}
 
try{
    pcntl_alarm(1);
    pcntl_signal(SIGALRM, "sig");
    a();
    pcntl_alarm(0);
}catch(Exception $e){
    echo "timeout ";
}
b();
a();
b();

对于文件或程序代码可使用set_time_limit只是设置你的PHP程序的超时时间.
解决方法:

1.修改php.ini文件,代码如下:

复制代码 代码如下:
max_execution_time = 30; // Maximum execution time of each script, in seconds 原值30秒,你可以改大点.

改完后记得重启php,该方法改了后对所有程序生效.

2.修改你的程序,在你需要耗时超过默认值(30秒)的脚本中,加入代码:

复制代码 代码如下:
set_time_limit(300); // 最大执行时间这里设置300秒

设置成0表示不限时。

希望本文所述对大家的PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!