Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask app hangs while processing the request

I have a simple flask app, single page, upload html and then do some processing on it on the POST; at POST request; i am using beautifulsoup, pandas and usually it takes 5-10 sec to complete the task.

at the end i export the resultant dataframe to excel with pandas(with the update of the previous stored excel if present). and on GET request i return the result of this dataframe.

Now issue is... app gives no response while those 5-10 sec.; even if i visit my app from another computer; it will show after the completion of those 5-10 sec. It means if any user of this app has uploaded his file; then rest others have to wait till his job completes.

i have even added the below mentioned code in my app; but no improvement.

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

if __name__ == '__main__':
    http_server = HTTPServer(WSGIContainer(app))
    http_server.listen(5657)
    IOLoop.instance().start()

also my system and python version is as below.. .

>>> sys.version
'2.7.5 |Anaconda 1.8.0 (32-bit)| (default, Jul  1 2013, 12:41:55) [MSC v.1500 32 bit (Intel)]'

Note: i want to move it to python3.3, and wanted to remain on my windows 7 machine!!

like image 829
namit Avatar asked Mar 11 '14 11:03

namit


2 Answers

Tornado is, typically, a single-threaded web server. If you write code specially for Tornado's asynchronous style you can process multiple requests concurrently, but in your case you aren't doing that; you're just using Tornado to serve requests with Flask, one at a time.

Remove Tornado and try using Flask's multithreaded option:

app.run(threaded=True)
like image 85
A. Jesse Jiryu Davis Avatar answered Nov 17 '22 13:11

A. Jesse Jiryu Davis


If you're using WSGI's run_simple function just add the threaded=true param.

Example:

run_simple('0.0.0.0', 9370, application, use_reloader=True, use_debugger=True, threaded=True)
like image 35
Nick Woodhams Avatar answered Nov 17 '22 15:11

Nick Woodhams