I have a rather large .csv file (up to 1 million lines) that I want to generate and send when a browser requests it.
The current code I have is (except that I don't actually generate the same data):
class CSVHandler(tornado.web.RequestHandler):
def get(self):
self.set_header('Content-Type','text/csv')
self.set_header('content-Disposition','attachement; filename=dump.csv')
self.write('lineNumber,measure\r\n') # File header
for line in range(0,1000000):
self.write(','.join([str(line),random.random()])+'\r\n') # mock data
app = tornado.web.Application([(r"/csv",csvHandler)])
app.listen(8080)
The problems I have with the method above are:
- The web browser doesn't directly start downloading chunks that are sent. It hangs while the webserver seems to prepare the whole content.
- The web server is blocked while it processes this request and makes other clients hang.