-1

I am encountering an issue where I am able to run a flask server when using python3 -m flask run, however it fails to start the server when attempting to launch through the app wsgi.py.

(Note: I'm trying to omit the project name and the like)

When using python3 -m flask run :

* Serving Flask app "wsgi.py" (lazy loading)
* Environment: config.DevelopmentConfiguration
* Debug mode: on
* Restarting with stat
* Debugger is active!
* Debugger PIN: ___-___-___
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)

When using python3 wsgi.py (freezes and does not exit):

* Serving Flask app "[PROJECT_NAME]" (lazy loading)
* Environment: config.DevelopmentConfiguration
* Debug mode: on

wsgi.py simply calls app.run() where app is imported from [PROJECT_NAME] where this code defines it:

app = Flask(__name__)
app.config.from_object( os.environ.get("FLASK_ENV") or 'config.DevelopmentConfiguration' 
)

app is also linked with some resources (e.g. database) and assigned blueprints but I am trying to avoid posting all the code I have, please let me know if you believe that information may be relevant. I noticed that the the app name is different between executions but that is the only lead I see and I am unaware of how that is being assigned but it is the only clue I see at the moment.

So far I have attempted to make the Flask() initializer take different values and attempted to make sure the correct environment is used but I think that is already correct. I would expect that the server functions the same way when launched via python3 wsgi.py as with python3 -m flask run.

1 Answers1

0

wsgi.py is not a piece of code intended to be run as a Python program, but to be loaded by the flask module as a Flask app. This means that what you call "running as a module" (which is, in fact, Flask starting a server and loading your app), is the way it is supposed to be done. "Running as a program" is not a mode you are supposed to use. In fact, being loaded by the flask module is only something that is done when you use Flask's internal server, which is recommended for development and debug, but not for production (I use gunicorn for my Flask production server).

At this point in time, when you already ran your first Flask app (which is a WSGI compliant web application), it is worth catching up on your reading about the whole WSGI framework. This framework specifies how different compliant component, like apps, servers and middleware can be used together to build, serve and deploy web sites. After doing that, you will find how you can write a program that initializes a WSGI server, and loads you WSGI app into it.

Amitai Irron
  • 1,973
  • 1
  • 12
  • 14