zoukankan      html  css  js  c++  java
  • 关于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

     

  • 相关阅读:
    linux系统空间不足,不重启进程,清理僵尸文件。
    python练习-使用163邮箱发送邮件
    python练习-(秒转时分秒,时分秒转秒)-对比linux中文件的上次更改时间跟当前时间相差多久。
    CentOS7 docker开启tcp端口并进行客户端远程连接
    zabbix 定义触发器,并使用邮件,微信消息报警。
    zabbix自定义监控,自定义图表。
    如何在linux中发送邮件,使用163邮箱发信。
    ansible-playbook的YAML语法学习
    将已有项目导入Gitlab
    ubuntu python PyCharm virtualenv
  • 原文地址:https://www.cnblogs.com/xiaozong/p/5742517.html
Copyright © 2011-2022 走看看