Reputation: 129
I write a very simple flask server. This server response to GET
and give back my home.html.
I visit the site on 127.0.0.1:5000
everything is good till now.
However, if I keep pressing "fresh" (Command+R on my computer) a lot of times for a few second, as fast as I can, then, my flask give this error and breaks down.
Exception in thread Thread-1: Traceback (most recent call last): File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 801, in bootstrap_inner self.run() File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 754, in run self.__target(*self.__args, **self.__kwargs) File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 659, in inner srv.serve_forever() File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 499, in serve_forever HTTPServer.serve_forever(self) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 238, in serve_forever self._handle_request_noblock() File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 297, in _handle_request_noblock self.handle_error(request, client_address) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock self.process_request(request, client_address) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 321, in process_request self.finish_request(request, client_address) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 334, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 655, in __init self.handle() File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 216, in handle rv = BaseHTTPRequestHandler.handle(self) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 340, in handle self.handle_one_request() File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 251, in handle_one_request return self.run_wsgi() File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 193, in run_wsgi execute(self.server.app) File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 184, in execute write(data) File "/usr/local/lib/python2.7/site-packages/werkzeug/serving.py", line 152, in write self.send_header(key, value) File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 401, in send_header self.wfile.write("%s: %s\r\n" % (keyword, value)) IOError: [Errno 32] Broken pipe
I believe this is what happened: When my server was trying to transmit html to my browser, I pressed the refresh and broke the pipe. So my server got confused and then give error.
How to solve this problem? Or this site is not usable as all since anyone who ask constantly for my page can break my webpage down.
Thanks!
Upvotes: 3
Views: 5353
Reputation: 1055
built-in werkzeug is not capable of handle concurrent requests the connection while the server is still churning its content out.
rather than using
app.run(debug=True,port=5000)
you should try this
from gevent.wsgi import WSGIServer
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()
Upvotes: 1
Reputation: 428
Which version of Flask are you using, version 0.12/0.13, or an older one?
You’re seeing this error because your server doesn’t handle concurrent requests. You need a proper WSGI server that is capable of handling concurrent requests ( use gunicorn or uWSGI in multithreaded environment) and it should work fine. Check: http://twistedmatrix.com/trac/wiki/TwistedWeb
What happens is that you are making a second request through reload to your flask app while it is still busy processing the first request. It won't respond to this second request until it is done with that first request. The requests.post() function is waiting for Flask to respond, but Flask itself is waiting for post()
Also enable threads in the Flask-supplied server using:
app.run(threaded=True)
Also note that Python 3 the socketserver implementation handles the disconnect more gracefully and continues to serve rather than crash.
Upvotes: 0
Reputation: 3664
I saw this error too in an Ubuntu Docker container on Kubernetes on Ubuntu VM:
Error on request:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 270, in run_wsgi
execute(self.server.app)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 261, in execute
write(data)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 227, in write
self.send_header(key, value)
File "/usr/lib/python2.7/BaseHTTPServer.py", line 412, in send_header
self.wfile.write("%s: %s\r\n" % (keyword, value))
IOError: [Errno 32] Broken pipe
I created a brand new Ubuntu xenial VM and ran the same code in Ubuntu Docker container on Kubernetes, and this error was not seen and Python Flask worked as expected. I think it was issue with my host (Ubuntu VM).
Upvotes: 1
Reputation: 175
you can try using app.run(threaded=True)
as suggested in this SO answer.
Upvotes: 3
Reputation: 188
This error appear because your server is overloaded! Stop and start it!
Upvotes: 1