【问题标题】:Call a Flask app with gunicorn from python module从 python 模块调用带有 gunicorn 的 Flask 应用程序
【发布时间】:2018-07-08 17:02:15
【问题描述】:

我想从类似的 CLI 启动一个 Web 应用程序:

 user@server:~$ my_app web --start

Web 项目是使用 Flask 开发的,我想在守护模式下使用 gunicorn 执行 Web 应用程序。

但我不明白如何从 python 模块执行 gunicorn 命令。命令是:

user@server:~$ gunicorn --bind 0.0.0.0:8000  wsgi:app --daemon

我想到了以下功能:

def start_server():
     command = "gunicorn --bind 0.0.0.0:8000 wsgi:app --daemon"
     subprocess.call(command, shell=True)

显然它不起作用。我想要一些允许我从 python 模块控制服务器(启动、状态、停止等)的东西。有可能吗?

项目结构:

├── my_app
│   ├── cli
│   │   ├── cli_app.py
│   │   ├── __init__.py
│   ├── helpers.py
│   ├── __version__.py
│   └── web
│       ├── __init__.py
│       ├── services.py
│       ├── static/
│       ├── templates/
│       │   ├── index.html
│       ├── services.py
│       ├── wsgi.py

【问题讨论】:

    标签: python flask gunicorn


    【解决方案1】:

    我用 os.chdir(here) 解决了它,其中 here 是 wsgi 模块所在的当前目录。

    类似这样的东西(server.py):

    import os
    import subprocess
    
    here = os.path.abspath(os.path.dirname(__file__))
    
    def start_server():
         os.chdir(here)
         command = "gunicorn --bind 0.0.0.0:8000 wsgi:app --daemon"
         subprocess.call(command, shell=True)
    

    目录结构为:

    ├── my_app
    │   ├── cli
    │   │   ├── cli_app.py
    │   │   ├── __init__.py
    │   ├── helpers.py
    │   ├── __version__.py
    │   └── web
    │       ├── __init__.py
    │       ├── services.py
    │       ├── static/
    │       ├── templates/
    │       │   ├── index.html
    │       ├── server.py
    │       ├── wsgi.py
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-03
      • 1970-01-01
      • 2012-02-15
      • 2018-03-10
      相关资源
      最近更新 更多