How to deploy Flask application using Gunicorn?
Flask is a lightweight Python Web framework that is widely used to develop various types of Web applications. Gunicorn (Green Unicorn) is a Python-based HTTP server used to run WSGI (Web Server Gateway Interface) applications. This article will introduce how to use Gunicorn to deploy Flask applications, and attach specific code examples.
Before we start, we need to make sure that python and pip tools have been installed in the system. Open a terminal and execute the following command to install Flask and Gunicorn:
$ pip install flask gunicorn
Create a file named app.py
in the project directory Python file, select a simple sample application to demonstrate Gunicorn deployment. The following is a code example for a simple Flask application:
from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return "Hello, Flask!" if __name__ == '__main__': app.run()
The above code creates a Flask application named app
and returns a simple Hello message on the root route.
Execute the following command in the terminal to test whether the Flask application is running properly:
$ python app.py
If everything goes well, you should be able to See output similar to the following:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Now, we have verified that the Flask application works properly.
Now we can use Gunicorn to start the Flask application. Execute the following command in the terminal:
$ gunicorn app:app
In the above command, app:app
means that the application to be started is app# in the
app.py file ##Object.
[2021-01-01 12:00:00 +0000] [12345] [INFO] Starting gunicorn 20.0.4 [2021-01-01 12:00:00 +0000] [12345] [INFO] Listening at: http://127.0.0.1:8000 (12345) [2021-01-01 12:00:00 +0000] [12345] [INFO] Using worker: sync [2021-01-01 12:00:00 +0000] [12345] [INFO] Booting worker with pid: 67890
http://127.0.0.1:8000/
The above is the detailed content of Gunicorn Deployment Guide for Flask Applications. For more information, please follow other related articles on the PHP Chinese website!