You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Bazel配置Gunicorn运行Python应用

使用Bazel配置Gunicorn运行Python Flask应用

问题背景

我正在开发一个Python项目,用Bazel做构建管理,想通过Gunicorn作为HTTP服务器运行Flask应用。已经在requirements.txt中添加了Gunicorn依赖,也配置了_pip_parse导入依赖,但不清楚如何调整Bazel配置让Gunicorn成为应用的运行器。现有配置如下:

requirements.txt

flask
gunicorn

WORKSPACE中的依赖配置

_pip_parse(
        name = "embeddings_requirements",
        python_interpreter_target = interpreter,
        requirements_lock = "//service:requirements.txt"
)

BUILD.bazel中的py_binary配置

load("@aspect_rules_py//py:defs.bzl", "py_binary")

py_binary(
    name = name,
    srcs = merged_srcs,
    data = merged_data,
    env = binary_env,
    imports = ["src", "."],
    main = main,
    tags = merged_tags,
    toolchains = merged_toolchains,
    visibility = merged_visibility,
    deps = merged_deps
)

解决方案

步骤1:引入Gunicorn依赖

在py_binary目标的deps中添加Gunicorn依赖,确保Bazel将其打包到构建产物中:

deps = merged_deps + ["@embeddings_requirements//:gunicorn"]

步骤2:配置Gunicorn运行方式

有两种常用方式实现Gunicorn运行Flask应用:

方式一:自定义运行脚本(推荐,灵活可控)

创建gunicorn_runner.py脚本,通过Gunicorn的API启动Flask应用:

# gunicorn_runner.py
from gunicorn.app.base import BaseApplication
# 替换为你的Flask应用模块路径和实例名
from src.my_app import app

class FlaskGunicornApp(BaseApplication):
    def __init__(self, app, options=None):
        self.options = options or {}
        self.application = app
        super().__init__()

    def load_config(self):
        for key, value in self.options.items():
            if key in self.cfg.settings and value is not None:
                self.cfg.set(key.lower(), value)

    def load(self):
        return self.application

if __name__ == "__main__":
    # 可根据需求调整Gunicorn参数
    gunicorn_options = {
        "bind": "0.0.0.0:8000",
        "workers": 4,
        "loglevel": "info"
    }
    FlaskGunicornApp(app, gunicorn_options).run()

更新py_binary配置,将脚本设为主入口并加入源码列表:

py_binary(
    name = "my_app",
    srcs = merged_srcs + ["gunicorn_runner.py"],
    data = merged_data,
    env = binary_env,
    imports = ["src", "."],
    main = "gunicorn_runner.py",
    tags = merged_tags,
    toolchains = merged_toolchains,
    visibility = merged_visibility,
    deps = merged_deps + ["@embeddings_requirements//:gunicorn"]
)

方式二:直接调用Gunicorn命令行

无需自定义脚本时,可直接创建调用Gunicorn的py_binary目标:

py_binary(
    name = "run_gunicorn",
    main = "gunicorn",
    deps = ["@embeddings_requirements//:gunicorn"],
    args = [
        "src.my_app:app",  # 替换为你的Flask应用模块路径和实例名
        "--bind", "0.0.0.0:8000",
        "--workers", "4"
    ],
    imports = ["src", "."],  # 确保Gunicorn能找到应用模块
)

步骤3:启动应用

通过Bazel命令运行:

# 方式一启动命令
bazel run //service:my_app

# 方式二启动命令
bazel run //service:run_gunicorn

注意事项

  • 确保Flask应用的模块路径正确,imports配置需包含源码目录
  • 可根据需求调整Gunicorn参数(如端口、worker数量、日志级别等)
  • 保证Bazel使用的Python解释器与项目环境一致

内容的提问来源于stack exchange,提问作者Lyov Ghukasyan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 22:42:06