Skip to content

Running Application

For running this application, we can use Flask's run method. But we should write this line of code on if __name__ == "__main__" statement. Why ?!

if "__name__" == "__main__" ?

Short Answer: this is the part that runs when the script is run from the CLI.

1
2
3
4
# ...

if __name__ == "__main__":
    app.run()

Development Server

Do not use run() in a production setting. It is not intended to meet security and performance requirements for a production server. Instead, see deployment for WSGI server recommendations ...

1
2
# read from here 👇
>>> Flask.run.__doc__
1
$ python <my_module>.py

APP_RUN

Another ways of running Flask app

Running from terminal:

1
2
$ export FLASK_APP=<module_name>
$ flask run
  • module_name - if your file's name is server.py, you should write server
  • flask run - flask has its built-in CLI 🔥
  • Reference: Flask CLI

You can set environments for your flask apps. If the environment is set to development, the flask command will enable debug mode and flask run will enable the interactive debugger and reloader.

1
2
3
$ export FLASK_ENV=development
$ export FLASK_APP=<module_name>
$ flask run

You do this in your code by passing debug=True in the run method:

1
app.run(debug=True)

FLASK_CLI_RUN

Gunicorn 🦄

GUNICORN_LOGO

Gunicorn

Gunicorn is a WSGI HTTP server for UNIX 🐧 You cannot use gunicorn server on your windows. But you will be able to run your applications by gunicorn using docker containers or WSL (windows subsystem for Linux)

Installation

1
$ pip install gunicorn

Running application:

1
$ gunicorn -w <workers> <your_module>:<your_app>
  • workers - it is good that settings workers number by this formula: workers = CPU_CORE * 2 + 1
  • your_module - if you wrote flask app's code in file.py, you should write file
  • your_app - if you defined your flask app as app in file.py, you should writeapp`
1
$ gunicorn -w 9 file:app

GUNICORN

Back to top