【问题标题】:How to create multiple instance of flask application in same server and same port如何在同一服务器和同一端口中创建烧瓶应用程序的多个实例
【发布时间】:2020-04-02 12:04:30
【问题描述】:

如何在同一网络服务器和同一端口中运行烧瓶应用程序的多个实例?

目前我的烧瓶应用程序具有以下目录结构:

├── app
│   ├── __pycache__
│   ├── static
│   └── templates
├── instance
├── src
    └── __pycache__

当应用程序开始使用flask run 时,它会在默认端口localhost:5000 上运行。

我能够创建应用程序的副本并在两个端口 5000 和 5001 上运行,并且运行良好。

但是,我只想使用一个带有 index.html 文件的服务器,其中 localhost:5000/index.html 有 2 个链接用于重定向到 app1 的 localhost:5000/app1/ 和 app2 的 localhost:5000/app2/

这是为了避免使用额外的端口。

我目前已将应用程序克隆到 app2,但不知道要运行两个应用程序。(以下是目录结构)

├── app
│   ├── __pycache__
│   ├── static
│   └── templates
├── app2
│   ├── __pycache__
│   ├── static
│   └── templates
├── instance
├── src
    └── __pycache__

我还检查了如何使用蓝图,但据我了解,它们旨在用于不同的视图。

【问题讨论】:

    标签: python flask


    【解决方案1】:

    您可以使用 werkzeug 中间件来执行此操作。它允许您在 /app1//app2/ 等挂载路径上挂载多个应用程序。

    您可以在 werkzeug 文档中阅读更多相关信息: https://werkzeug.palletsprojects.com/en/0.14.x/middlewares/

    【讨论】:

    • 好吧,我还不能解决它,但我觉得这是最接近的解决方案。所以,努力吧。
    【解决方案2】:

    您无需使用两个应用程序。您可以让 index.html 包含以下代码行

    @app.route('/app1')
    def hello():
        return render_template('app1.html')
    
    @app.route('/app2')
    def hello():
        return render_template('app2.html')
    

    这意味着您可以按照指定 (localhost:5000/app1) 访问 app1 或 app2,而无需创建多个应用

    【讨论】:

    • 这里的想法是不要有两个不同的 HTML。但是要创建应用程序的两个实例。
    • @AshwinGeetD'Sa 你不能在同一个端口上创建应用程序的多个实例
    【解决方案3】:

    我想说您可以在不同的端口上运行 2 个应用程序,并使用反向代理对它们进行负载平衡。 Check out this guide.

    作为替代方案,您可以使用 gunicorn 之类的内容运行应用的多个实例。

    【讨论】:

    • 但是我们不能使用 gunicorn 在单个端口中运行应用程序的多个实例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 2020-07-06
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多