简体   繁体   English

PHP-FPM - 限制执行时间,优雅停止

[英]PHP-FPM - Limit execution time, graceful stop

So I have, in my application, some third-party calls to webservices. 所以我在我的应用程序中有一些第三方调用webservices。

These services can take, on peak times, a lot of time to respond, such that the value I defined for request_terminate_timeout on my PHP-FPM setup is reached. 这些服务可以在高峰时间响应大量时间,以便达到我在PHP-FPM设置上为request_terminate_timeout定义的值。 Script is then brutally aborted, and I cannot make use of register_shutdown_function to attempt a cleanup of the request. 然后,脚本被残酷地中止,我无法使用register_shutdown_function来尝试清理请求。

Is there any PHP/PHP-FPM setting I'm not aware of that could limit the total execution time of a request? 是否有任何PHP / PHP-FPM设置我不知道这可能会限制请求的执行时间? A "graceful" way to handle PHP-FPM termination? 处理PHP-FPM终止的“优雅”方式?

  • max_execution_time only takes into account time spent in PHP, which in my case is not the cause of long-running scripts max_execution_time只考虑在PHP中花费的时间,在我的情况下,这不是长时间运行脚本的原因
  • Using pcntl calls, for web requests, doesn't seem such a good idea. 使用pcntl调用,对于Web请求,似乎不是一个好主意。

For a long-polling requests I would suggest to use cron/stack approach. 对于长轮询请求,我建议使用cron / stack方法。

Question, why it was stopped could be addressed to your Apache/nginx server: 问题,为什么它被停止可以发送到你的Apache / nginx服务器:

proxy_connect_timeout 500;
proxy_send_timeout 500;
proxy_read_timeout 500;

Also, you could use their triggers to handle timeout errors. 此外,您可以使用其触发器来处理超时错误。

try setting the max_execution_time and catch the error 尝试设置max_execution_time并捕获错误

<?php
function shutdown()
{
    $a=error_get_last();
    if($a==null) {
        echo "No errors";
    } else {
        gracefully_shutdown();
    }
}
register_shutdown_function('shutdown');
ini_set('max_execution_time', 10 ); //set to 10 seconds

// do some works here
do_some_works();

?>

have a look at this link http://www.php.net/manual/en/function.set-error-handler.php#106061 请看这个链接http://www.php.net/manual/en/function.set-error-handler.php#106061

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM