tornado+gunicorn部署设置max_body_size

背景

想通过 gunicornmax_requests 配置实现重启进程,解决一些内存泄露问题。

因为gunicorn启动配置的是 tornado.web.Application 实例,并非直接使用 tornado.httpserver.HTTPServer 导致无法设置 max_body_sizemax_buffer_size

gunicore配置项 中只有 limit_request_line /limit_request_fields / limit_request_field_size 无法满足 tornado的限制配置。

现状

依赖版本如下:

  • Python3.9
  • tornado==6.5.2
  • gunicorn==23.0.0

app.py

python 复制代码
# 定义实例
app = tornado.web.Application(url_patterns)

gunicorn.conf.py

python 复制代码
# 因为用的docker部署有restart配置
daemon = False
# 不能设置为True, tornado的IOLoop不能被共享
preload_app = False
# 可以有效解决OOM的问题
max_requests = 102400
max_requests_jitter = 1024
# 连接的空闲时间(秒),对应 idle_connection_timeout=60
keepalive = 60
# 对应 body_timeout/请求处理超时
timeout = 60
# bind
# port = os.environ.get("PROJECT_PORT", 8000)
# bind = "0.0.0.0:{}".format(port)
worker_class = "tornado"

启动命令

复制代码
gunicorn -c gunicorn.conf.py --bind 0.0.0.0:8000 --workers 2 app:app

自定义 TornadoWorker

解决方式可以通过自定义 TornadoWorker . 代码是基于 gunicorn 源码修改的,主要修改 server_class 初始化的部分,增加了 max_body_size 相关配置。

最终的是同步修改 gunicore 启动 worker_class 配置:

python 复制代码
worker_class = "gunicorn_worker.MyTornadoWorker"

gunicorn_worker 代码实现如下:

python 复制代码
#!/usr/bin/env python
# coding=utf-8
import typing

from gunicorn.sock import ssl_context
from gunicorn.workers.gtornado import TornadoWorker

import tornado
from tornado.wsgi import WSGIContainer
from tornado.ioloop import IOLoop, PeriodicCallback

MAX_FILE_SIZE = 100 * 1024 * 1024  # 100MB

class MyTornadoWorker(TornadoWorker):
    """专门为tornado>=6定制
    因为需要实现设置 max_body_size
    """

    def run(self) -> None:
        self.ioloop = IOLoop.instance()
        self.alive = True
        self.server_alive = False

        # tornado >= 5
        self.callbacks = []
        self.callbacks.append(PeriodicCallback(self.watchdog, 1000))
        self.callbacks.append(PeriodicCallback(self.heartbeat, 1000))
        for callback in self.callbacks:
            callback.start()
        # Assume the app is a WSGI callable if its not an
        # instance of tornado.web.Application or is an
        # instance of tornado.wsgi.WSGIApplication
        app = self.wsgi

        if not isinstance(app, WSGIContainer) and not isinstance(app, tornado.web.Application):
            app = WSGIContainer(app)

        # Monkey-patching HTTPConnection.finish to count the
        # number of requests being handled by Tornado. This
        # will help gunicorn shutdown the worker if max_requests
        # is exceeded.
        httpserver = tornado.httpserver.HTTPServer
        if hasattr(httpserver, "HTTPConnection"):
            old_connection_finish = httpserver.HTTPConnection.finish

            def finish(other: typing.Any) -> None:
                self.handle_request()
                old_connection_finish(other)

            httpserver.HTTPConnection.finish = finish

            server_class = httpserver
        else:

            class _HTTPServer(tornado.httpserver.HTTPServer):
                def on_close(instance: typing.Any, server_conn: typing.Any) -> None:
                    self.handle_request()
                    super().on_close(server_conn)

            server_class = _HTTPServer

        if self.cfg.is_ssl:
            server = server_class(
                app,
                ssl_options=ssl_context(self.cfg),
                max_body_size=MAX_FILE_SIZE,
                max_buffer_size=MAX_FILE_SIZE,
            )
        else:
            server = server_class(
                app,
                max_body_size=MAX_FILE_SIZE,
                max_buffer_size=MAX_FILE_SIZE,
            )

        self.server = server
        self.server_alive = True

        for s in self.sockets:
            s.setblocking(0)
            if hasattr(server, "add_socket"):  # tornado > 2.0
                server.add_socket(s)
            elif hasattr(server, "_sockets"):  # tornado 2.0
                server._sockets[s.fileno()] = s

        server.no_keep_alive = self.cfg.keepalive <= 0
        server.start(num_processes=1)
        self.ioloop.start()
相关推荐
二川bro7 小时前
量子计算入门:Python量子编程基础
python
夏天的味道٥8 小时前
@JsonIgnore对Date类型不生效
开发语言·python
tsumikistep8 小时前
【前后端】接口文档与导入
前端·后端·python·硬件架构
小白学大数据8 小时前
Python爬虫伪装策略:如何模拟浏览器正常访问JSP站点
java·开发语言·爬虫·python
头发还在的女程序员10 小时前
三天搞定招聘系统!附完整源码
开发语言·python
温轻舟10 小时前
Python自动办公工具06-设置Word文档中表格的格式
开发语言·python·word·自动化工具·温轻舟
花酒锄作田10 小时前
[python]FastAPI-Tracking ID 的设计
python·fastapi
AI-智能10 小时前
别啃文档了!3 分钟带小白跑完 Dify 全链路:从 0 到第一个 AI 工作流
人工智能·python·自然语言处理·llm·embedding·agent·rag
d***956212 小时前
爬虫自动化(DrissionPage)
爬虫·python·自动化
APIshop12 小时前
Python 零基础写爬虫:一步步抓取商品详情(超细详解)
开发语言·爬虫·python