I am running a flask web server, it works fine during testing, but now freezes at least once per day. All I need to do is to restart it and it will work again. Is there a good way to monitor it and maybe I should just kill/restart it every time it fails. Do people actually kill their web server periodically to avoid this kind thing from happening?
When Flask app runs slow we need to identify what is the bottleneck. It can be an overloaded database, unresponsive external API, or heavy, CPU-intensive computation.
Although Flask has a built-in web server, as we all know, it's not suitable for production and needs to be put behind a real web server able to communicate with Flask through a WSGI protocol. A common choice for that is Gunicorn—a Python WSGI HTTP server.
If you are using the default Flask webserver: Don't. It's intended for development ONLY.
As additional resource it's worth reading these two blog posts about deploying a Flask application:
http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux-even-on-the-raspberry-pi http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xviii-deployment-on-the-heroku-cloud
And for monitoring processes in your webserver, you could give Watchy a try:
http://redbrain.github.io/watchy/
While the default web server might not be the best for production, it is probably not the root cause of the crashes. I use it in a production environment on an internal network and it is very stable. Before blaming the web server check to make sure your code can handle requests that that may collide with each other. In my case I had lots of stability issues before I instituted locking on data base tables so that certain requests would be blocked until previous requests were done with updates. Flask can't make sure your code is thread safe. And changing the web server won't help if it is not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With