在运行WSGI服务器之前在gunicorn中运行安装脚本

问题描述 投票:-1回答:1

说我有一个模块setup.py,在server.py中有一些烧瓶应用程序代码。

说我想运行gunicorn -b :$PORT server:app。但是我想先在setup.py中运行逻辑,然后再设置WSGI服务器并在server.py中运行任何其他逻辑。

[似乎没有记录到任何用粗麻布制成的旗帜,让我来进行此操作(除非我丢失了某些东西)。基本上我想做类似gunicorn --RunThisFirst setup.py -b :$PORT server:app

的操作
python flask gunicorn
1个回答
0
投票

使用gunicorn.conf.py文件应该可以解决问题。

import multiprocessing
import os

###########
# INSERT / IMPORT setup code
#
# maybe like this...
#
# from src.setup import run_setup
#
# run_setup()
###########

port = os.getenv("PORT", 8000)
bind = f"127.0.0.1:{port}" 
workers = multiprocessing.cpu_count() * 2 + 1

正在运行的服务器:

gunicorn --check-config server:app

在您独特的用例中,您可能需要将gunicorn指向哪里(请参见docs):

gunicorn -c /path/to/gunicorn.config.py server:app

链接到与使用gunicorn.config.py相关的文档:https://docs.gunicorn.org/en/latest/configure.html#configuration-file

© www.soinside.com 2019 - 2024. All rights reserved.