Jon*_*n G 11 python gunicorn docker docker-compose
我发现当我使用 docker-compose 关闭我的Gunicorn (19.7.1) python 应用程序时,总是需要 10 秒才能关闭。这是 docker-compose 在强制杀死进程之前等待的默认最大时间(通过-t / --timeout参数调整)。我认为这意味着gunicorn 没有被优雅地关闭。我可以用以下方法重现这个:
docker-compose.yml:
version: "3"
services:
test:
build: ./
ports:
- 8000:8000
Run Code Online (Sandbox Code Playgroud)
Dockerfile:
FROM python
RUN pip install gunicorn
COPY test.py .
EXPOSE 8000
CMD gunicorn -b :8000 test:app
Run Code Online (Sandbox Code Playgroud)
测试.py
def app(_, start_response):
"""Simplest possible application object"""
data = b'Hello, World!\n'
status = '200 OK'
response_headers = [
('Content-type', 'text/plain'),
('Content-Length', str(len(data)))
]
start_response(status, response_headers)
return iter([data])
Run Code Online (Sandbox Code Playgroud)
然后运行应用程序:
docker-compose up -d
Run Code Online (Sandbox Code Playgroud)
并优雅地停止它:
docker-compose stop
Run Code Online (Sandbox Code Playgroud)
版本:
docker-compose version 1.12.0, build b31ff33
Run Code Online (Sandbox Code Playgroud)
我宁愿让gunicorn优雅地停下来。我认为它应该能够基于base.py中的信号处理程序。
上述所有内容对于使用两次更新图像也适用docker-compose up -d,第二次用新图像替换旧图像。
我是否误解/误用了什么?docker-compose 发送什么信号来停止进程?Gunicorn不应该使用它吗?我应该能够以 10 秒以内的速度重新启动应用程序吗?
det*_*lba 15
exec在 dockerfile 中的 CMD 之后添加: CMD exec gunicorn -b :8000 test:app。
我遇到了同样的问题,当我运行时docker exec my_running_gunicorn ps aux,我看到类似的内容:
gunicorn 1 0.0 0.0 4336 732 ? Ss 10:38 0:00 /bin/sh -c gunicorn -c gunicorn.conf.py vision:app
gunicorn 5 0.1 1.1 91600 22636 ? S 10:38 0:00 /usr/local/bin/python /usr/local/bin/gunicorn -c gunicorn.conf.py vision:app
gunicorn 8 0.2 2.5 186328 52540 ? S 10:38 0:00 /usr/local/bin/python /usr/local/bin/gunicorn -c gunicorn.conf.py vision:app
Run Code Online (Sandbox Code Playgroud)
1 PID不是gunicorn master,因此它没有收到sigterm信号。
通过execDockerfile 中的 ,我现在有了
gunicorn 1 32.0 1.1 91472 22624 ? Ss 10:43 0:00 /usr/local/bin/python /usr/local/bin/gunicorn -c gunicorn.conf.py vision:app
gunicorn 7 45.0 1.9 131664 39116 ? R 10:43 0:00 /usr/local/bin/python /usr/local/bin/gunicorn -c gunicorn.conf.py vision:app
Run Code Online (Sandbox Code Playgroud)
它有效。
| 归档时间: |
|
| 查看次数: |
3931 次 |
| 最近记录: |