简体   繁体   中英

gunicorn Connection in use for python flask

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM