88
99from tornado import gen
1010from tornado .web import HTTPError
11- from tornado .iostream import StreamClosedError
1211from json import dumps
1312
1413import qiita_db as qdb
@@ -67,6 +66,7 @@ async def get(self, analysis_id):
6766 """
6867 chunk_len = 1024 * 1024 * 1 # 1 MiB
6968
69+ respose = None
7070 with qdb .sql_connection .TRN :
7171 a = _get_analysis (analysis_id )
7272 mf_fp = qdb .util .get_filepath_information (
@@ -76,17 +76,19 @@ async def get(self, analysis_id):
7676 mf_fp , index = '#SampleID' )
7777 response = dumps (df .to_dict (orient = 'index' ))
7878
79- crange = range (chunk_len , len (response )+ chunk_len , chunk_len )
80- for i , (win ) in enumerate (crange ):
81- chunk = response [i * chunk_len :win ]
82- try :
83- self .write (chunk )
84- await self .flush ()
85- except StreamClosedError :
86- break
87- finally :
88- del chunk
89- # pause the coroutine so other handlers can run
90- await gen .sleep (0.000000001 ) # 1 nanosecond
91- else :
92- self .write (None )
79+ if respose is not None :
80+ crange = range (chunk_len , len (response )+ chunk_len , chunk_len )
81+ for i , (win ) in enumerate (crange ):
82+ # sending the chunk and flushing
83+ chunk = response [i * chunk_len :win ]
84+ self .write (chunk )
85+ await self .flush ()
86+
87+ # cleaning chuck and pause the coroutine so other handlers
88+ # can run, note that this is required/important based on the
89+ # original implementation in https://bit.ly/3CPvyjd
90+ del chunk
91+ await gen .sleep (0.000000001 ) # 1 nanosecond
92+
93+ else :
94+ respose .write (None )
0 commit comments