WSGI server: Gunicorn
Once you've completed development on your application, you'll aim to deploy it for public access. During local development, you likely rely on the integrated development server, debugger, and reloader, which are not suitable for production environments. Instead, opt for a dedicated WSGI server or hosting platform.
Flask is a WSGI application. A WSGI server is used to run the application, converting incoming HTTP requests to the standard WSGI environ, and converting outgoing WSGI responses to HTTP responses.
Use Gunicorn
Gunicorn is a pure Python WSGI server with simple configuration and multiple worker implementations for performance tuning.
Install gunicorn
pip3 install gunicorn
Run with gunicorn
gunicorn -w 4 -b :5000 main:app
If you want to see logs, add
--log-level debug
option.
The -w
option specifies the number of processes to run; a starting value could be CPU * 2
.
The -b
option binds Gunicorn to listen on port 5000.
main:app
specifies the module and application variable name. main
is the name of the Python file (main.py
) and app
is the variable holding your Flask application.
Make sure you have the config.yml
config file along with your main.py
, and all yours databases are ready to serve.
Gunicorn's start logs look like this:
[2024-03-08 22:16:31 +0800] [75236] [INFO] Starting gunicorn 21.2.0
[2024-03-08 22:16:31 +0800] [75236] [INFO] Listening at: http://0.0.0.0:5000 (75236)
[2024-03-08 22:16:31 +0800] [75236] [INFO] Using worker: sync
[2024-03-08 22:16:31 +0800] [75238] [INFO] Booting worker with pid: 75238
[2024-03-08 22:16:31 +0800] [75239] [INFO] Booting worker with pid: 75239
[2024-03-08 22:16:31 +0800] [75240] [INFO] Booting worker with pid: 75240
[2024-03-08 22:16:31 +0800] [75241] [INFO] Booting worker with pid: 75241
Send some requests with curl.
curl -X GET "http://localhost:5000/books?o=5"
You will see something like this in the response:
[{"author":"Jane Austen","created_at":"2024-03-02T05:48:25","description":"A classic novel exploring the themes of love, reputation, and social class in Georgian England.","id":7,"isbn":"9780486284736","published_at":"1813-01-28","title":"Pride and Prejudice","total_pages":279,"updated_at":"2024-03-02T05:48:25"},{"author":"J.D. Salinger","created_at":"2024-03-02T05:48:25","description":"A novel narrated by a disaffected teenager, exploring themes of alienation and identity.","id":8,"isbn":"9780316769488","published_at":"1951-07-16","title":"The Catcher in the Rye","total_pages":277,"updated_at":"2024-03-02T05:48:25"},{"author":"J.R.R. Tolkien","created_at":"2024-03-02T05:48:25","description":"A high fantasy epic following the quest to destroy the One Ring and defeat the Dark Lord Sauron.","id":9,"isbn":"9780544003415","published_at":"1954-07-29","title":"The Lord of the Rings","total_pages":1178,"updated_at":"2024-03-02T05:48:25"},{"author":"Herman Melville","created_at":"2024-03-02T05:48:25","description":"A novel exploring themes of obsession, revenge, and the nature of good and evil.","id":10,"isbn":"9780142000083","published_at":"1851-10-18","title":"Moby-Dick","total_pages":624,"updated_at":"2024-03-02T05:48:25"},{"author":"J.R.R. Tolkien","created_at":"2024-03-02T05:48:25","description":"A fantasy novel set in Middle-earth, following the adventure of Bilbo Baggins and the quest for treasure.","id":11,"isbn":"9780345339683","published_at":"1937-09-21","title":"The Hobbit","total_pages":310,"updated_at":"2024-03-02T05:48:25"}]
It works like a charm! ⭐️