8
8
9
9
from tornado import gen
10
10
from tornado .web import HTTPError
11
- from tornado .iostream import StreamClosedError
12
11
from json import dumps
13
12
14
13
import qiita_db as qdb
@@ -67,6 +66,7 @@ async def get(self, analysis_id):
67
66
"""
68
67
chunk_len = 1024 * 1024 * 1 # 1 MiB
69
68
69
+ respose = None
70
70
with qdb .sql_connection .TRN :
71
71
a = _get_analysis (analysis_id )
72
72
mf_fp = qdb .util .get_filepath_information (
@@ -76,17 +76,19 @@ async def get(self, analysis_id):
76
76
mf_fp , index = '#SampleID' )
77
77
response = dumps (df .to_dict (orient = 'index' ))
78
78
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