【php】php-fpm的中断和启动控制

php-fpm,如何控制访问特别长的请求?是否有中断的机制?

 

max_execute_time(php.ini): 这个是用户态时间,不累计io操作,但是大多数操作都是io会慢,所以这个参数只能适用于cpu密集型,大部分应用不适合;

request_terminate_timeout(php-fpm): 这个是request整个处理超时,是计算io操作的,

request_terminate_timeout,设置为5s,超过这个时间点,php进程结束,然后给客户端返回502.

 

php-fpm对于这两种设置的处理都是直接kill掉进程,再启动一个新的worker进程,整个操作代价是比较高的,但是重启有个好处是内存不会被不断消耗,即使有小部分泄漏都没事。

 

需要注意的是:max_execute_time是php.ini中设置的,request_terminate_timeout是php-fpm中设置的。 

 

https://www.kancloud.cn/nickbai/php7/363357

 

另外,如果想解决这种超时问题,业务层面的php代码优化,接口优化才是处理超时的最终解决方案。

 

 

二,其他php-fpm参数

; Possible Values:
;   static  - a fixed number (pm.max_children) of child processes;
;   dynamic - the number of child processes are set dynamically based on the
;             following directives. With this process management, there will be
;             always at least 1 children.
;             pm.max_children      - the maximum number of children that can
;                                    be alive at the same time.
;             pm.start_servers     - the number of children created on startup.
;             pm.min_spare_servers - the minimum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is less than this
;                                    number then some children will be created.
;             pm.max_spare_servers - the maximum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is greater than this
;                                    number then some children will be killed.
pm = dynamic
pm.max_children = 5
pm.min_spare_servers = 1
pm.max_spare_servers = 2

; The number of requests each child process should execute before respawning.
; This can be useful to work around memory leaks in 3rd party libraries. For
; endless request processing specify '0'. Equivalent to PHP_FCGI_MAX_REQUESTS.
; Default Value: 0
pm.max_requests = 500

; 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. A value of '0' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_terminate_timeout = 5



 

pm相关的参数,都是和进程管理相关的。

pm=dynamic表示进程数是动态生成。

pm.max_children 表示允许生成的最大子进程数

pm.min_spare_children 表示最小的空闲子进程

pm.max_spare_children 表示最大的空闲子进程

pm.max_requests = 500; 整个代表的是每个子进程在重新生成之前要处理多少个请求数。这个对于内存泄漏有很好的抑制作用。

也就是说子进程在处理到500个请求时候一定会重新生成。

 

php-fpm,默认在处理多个并发请求时,如果当前进程不够用时,最多能开到max_children个子进程数。

但是这些临时生成的子进程,并不是请求完毕就会释放,而是得等到因为某些原因需要退出时才会退出。

常见的有两个原因:

A,设定了pm.max_requests参数,那么处理了这么多请求一定会退出;

B,如设定了request_terminate_timeout,一个请求超过了这个限定,该对应的php-fpm子进程也会被kill掉。

 

posted on 2019-07-17 15:42  awildfish  阅读(327)  评论(0编辑  收藏  举报

导航