Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Flask webserver stop responding

Tags:

python

flask

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?

like image 988
Yifan Zhang Avatar asked Jul 22 '14 10:07

Yifan Zhang


People also ask

Why is Flask so slow?

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.

Does Flask need a web server?

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.


2 Answers

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/

like image 148
lol Avatar answered Sep 27 '22 19:09

lol


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.

like image 42
Clark Landis Avatar answered Sep 27 '22 18:09

Clark Landis