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 |
|
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 |
|
1 |
|
Another ways of running Flask app
Running from terminal:
1 2 |
|
module_name
- if your file's name isserver.py
, you should writeserver
flask run
- flask has its built-inCLI
🔥- 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 |
|
You do this in your code by passing debug=True
in the run
method:
1 |
|
Gunicorn 🦄
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 |
|
Running application:
1 |
|
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 infile.py
, you should writefile
your_app
- if you defined your flask app asapp
in file.py, you should write
app`
1 |
|