gunicorn Connection in use for python flask

青春壹個敷衍的年華 提交于 2019-12-08 11:22:34

问题


I recently changed my Heroku Python Flask app from the 'small application' format to the 'simple package' format based from flask documentation (De-coupling everything in app.py into separate subdirectories)

The application runs correctly using

> python runserver.py

However, executing

gunicorn runserver:app --log-file=- 

outputs:

"Starting gunicorn .... connection in use error" (loops forever)

My runserver.py configuration is:

 from re3 import app
 app.run(debug=True)

__init__.py configuration:

import os
from flask import Flask
from flask import render_template
app = Flask(__name__)
import views

view.py configuration:

from re3 import app
@app.route('/')
def index():
    return 'Hello World!'

What is changing in the two executions?


回答1:


The problem is that you run your application anytime runserver is imported. You only want that to happen when it's executed directly.

from re3 import app

if __name__ == '__main__':
    app.run(debug=True)

Edit:

The usage for gunicorn is

$ gunicorn [OPTIONS] APP_MODULE

When you run gunicorn, it imports APP_MODULE. In your case, you've specified runserver. So while you don't import it yourself, gunicorn does. And before gunicorn can run app, runserver runs it.



来源:https://stackoverflow.com/questions/35145728/gunicorn-connection-in-use-for-python-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!