dongnanman9093 2017-02-07 15:35
浏览 34
已采纳

如何在PHP脚本失败时避免服务器不响应

I can´t find an answer for this, probably I'm not searching with the right words.

When a PHP script fails (error in script, infinite loops, endless SQL calls, etc) the whole server goes "busy". It's like all the resources are taken and focus on trying to execute that script. By restarting apache/nginx the server goes back to normal.

How could this be avoided? Setting the timeout for the script won't solve it because if it's even 10 seconds timeout the server will be irresponsive for those 10 seconds to everybody.

Is there any way to avoid this happening? Yes, solving the script issues the problem will stop happening but I am sure there is a way to protect against this on the server side.

Just now an example comes to my mind. This script used to get the country code by calling a service in that website. For some reason the website would not serve to us randomly so the script would wait forever to receive the file contents.

$getcountry = file_get_contents('http://ip-api.com/php/'. getUserIP());

Thanks

  • 写回答

1条回答 默认 最新

  • doumu5934 2017-02-07 15:40
    关注

    Setting the timeout for the script won't solve it because if it's even 10 seconds timeout the server will be irresponsive for those 10 seconds to everybody.

    Not if your server is set up properly. Setting the PHP timeout is the correct way to handle runaway scripts (aside from fixing the actual cause of the problem). Fatal script errors will terminate immediately and not take up server resources.

    Your post seems to assume there is only one instance of a PHP script running at any time. This should not be the case. Your web server should be starting new processes or threads, up to some configured limit, for each web request handled by PHP. Or if you are using PHP-FPM, it's handed off to a pool of PHP processes. In no case should your whole server be locked up for one single request.

    Now if your code or server has serious issues, then all requests may take too long and hang up further requests. The only solution is to fix the root of the problem and keep a reasonable timeout as a failsafe.

    For Apache these are some of the settings you will want to check: ServerLimit, StartServers, MaxClients. For PHP-FPM you would start with the max_children setting.

    This script used to get the country code by calling a service in that website. For some reason the website would not serve to us randomly so the script would wait forever to receive the file contents.

    This is a very common problem and must be accounted for by your code. You can never trust that a 3rd party service will return promptly. Obviously offloading requests such as this to background processes is best (e.g. crons). But if it needs to be done as part of a normal page request you should always set a reasonable timeout. For your specific example of using file_get_contents, reduce the socket timeout:

    ini_set('default_socket_timeout', 10);  // 10 seconds
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据