Home > Article > Backend Development > Gunicorn Deployment Guide for Flask Applications
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: 67890Step 5: Test the Gunicorn deployed applicationNow, we need to test Let’s see if Gunicorn successfully deployed our Flask application. Open the following address in the browser:
http://127.0.0.1:8000/If everything goes well, you should be able to see the "Hello, Flask!" information returned by the Flask application we defined previously displayed on the browser page. ConclusionThrough the above simple steps, we successfully used Gunicorn to deploy Flask applications. Gunicorn provides high-performance, stable and scalable services that can provide better performance and availability for Flask applications. I hope this article can help you successfully deploy your Flask application. Reference link
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!