John
John

Reputation: 26

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?

Upvotes: 0

Views: 1557

Answers (1)

dirn
dirn

Reputation: 20739

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.

Upvotes: 6

Related Questions