• 关于PHP执行超时的问题


    PHP配置文件的参数max_execution_time表示脚本执行超时时间

    max_execution_time=0表示不限制

    max_execution_time=2表示执行两秒后终止,同时报错Fatal error: Maximum execution time of 2 seconds exceeded

    但是,sleep(10)函数是不起作用的。

    例如,php.ini中的max_execution_time=2

    执行代码 <?php sleep(10);echo 'ok'; ?> 是不会超时的(前提是web服务器不超时)

    执行代码 <?php echo 1;for($i=0;$i<100000000000;$i++){ for($i=0;$i<100000000000;$i++){}} echo 2;?> 会超时,输出1,且报错

    注:set_time_limit()函数是可以覆盖max_execution_time参数的,可以比max_execution_time参数大,也可以为0表示不限制,当然

    当此函数被调用时,set_time_limit()会从零开始重新启动超时计数器。换句话说,如果超时默认是30秒,在脚本运行了了25秒时调用 set_time_limit(20),那么,脚本在超时之前可运行总时间为45秒


    还有一种情况

    如果PHP工作在php-fpm模式,例如web服务器是nginx

    这时候的超时还取决于php-fpm.conf中的配置request_terminate_timeout

    这个参数是这么解释的:the timeout for serving a single request after which the worker process will be killed. This option should be used when the 'max_execution_time' ini option does not stop script execution for some reason. 

    大概的意思是如果max_execution_time这个参数没有限制脚本的执行,就取决于这个参数的超时,例如,max_execution_time=0;或者max_execution_time大于request_terminate_timeout的值,或者程序中有sleep(),最终的超时都取决于request_terminate_timeout参数;

    当然如果程序是因为request_terminate_timeout超时是不会有内容输出的,会报502错误,502 Bad Gateway/Nginx 1.7


    综上,总结下列对照表,如果有不对的地方,欢迎大家指正

     

    非fpm

    max_execution_time=4

    fpm

    request_terminate_timeout=8

    max_execution_time=4

    fpm

    request_terminate_timeout=4

    max_execution_time=8

    fpm

    request_terminate_timeout=4

    max_execution_time=0

    <?php

    echo 1;sleep(10);echo 2;

    ?>

    输出:12 输出:502 Bad Gateway  输出:502 Bad Gateway  输出:502 Bad Gateway

    <?php

    echo 1;$i=0;

    while($i++<1000000000)

    {//假设会执行很久}

    echo 2;

    ?>

    输出:1

    Fatal error: Maximum execution

    time of 4 seconds exceeded

    输出:1 

    Fatal error: Maximum execution

    time of 4 seconds exceeded

     输出:502 Bad Gateway  输出:502 Bad Gateway

     

  • 相关阅读:
    shell awk
    spl_autoload_register array参数
    install xdebug on fedora
    call_user_func
    转载 shell sort
    通过nginx + lua来统计nginx上的监控网络请求和性能
    nginx 知识点
    python 发送带附件的 邮件
    xdebug php
    关于ZendStudio 10.5的破解
  • 原文地址:https://www.cnblogs.com/xiaozong/p/5742517.html
Copyright © 2020-2023  润新知