In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how Flask deploys services using Gunicorn. It is concise and easy to understand. It will definitely make your eyes shine. I hope you can learn something from the detailed introduction of this article.
During development, we use the Flask Run command to start the development server to run the application. The server is provided by Werkzeug, but the server performance is relatively low and can only be used for development testing. It is not recommended to use it directly in the production environment. A high-performance server is needed to deploy Flask service. Gunicorn is a recommended high-performance server. Let's talk about it in detail.
1、Gunicorn
Gunicorn (Green Unicorn) is a Python WSGI HTTP server that only runs on Unix/Linux systems, derived from Ruby's Unicorn project. It uses the pre-fork worker model, at startup, will fork out a specified number of worker processes in the main process to process requests, greatly improving the server request load capacity, but also compatible with a variety of Python Web frameworks, simple implementation, less system resources, speed is also quite fast, so by the majority of Python developers love.
Guncorn installation is also very simple, using pip install guncorn command can be
2. Use of Gunicorn
Gunicorn can be run using the following command format:
unicorn [OPTIONS] ModuleName: AppInstance Name
For example, a simple Flask app's app.py file:
from flask import Flaskapp = Flask(__name__)@app.route("/")def index(): return "hello world"if __name__ == '__main__': app.run()
You can launch the app with the following command:
gunicorn -w 4 -b 127.0.0.1:8888 app:app
-w: is the number of open processes
-b: Specify listening address and port
app:app: The first app refers to the name of the Python module file, and the second refers to the name of the Flask application instance.
The above command represents running Flask application using 4 worker processes bound to local port 8888.
Guncorn actually has a lot of configuration items, which can be viewed through guncorn-h.
In fact, running Gunicorn by command is cumbersome because of the number of configuration items that need to be specified at runtime, usually by writing configuration items to the gunicorn.config.py file.
For example:
#log configuration loglevel = "debug"accesslog = './ log/access.log'errorlog = './ log/error.log'workers = 4#Set work mode to coroutine worker_class = "gevent"bind = "0.0.0: 8080"
The above configuration items such as log and working mode are configured, and the working mode is set to gevent, which is an asynchronous working mode based on Python third-party library gevent. In addition, there are sync, eventlet, tornado, gthread, and sync by default.
After configuration, use gunicorn app: app-c./ The gunicorn. Config. py command runs the Flask service using gunicorn.
That's how Flask deploys services using Gunicorn. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.