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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办