@@ -23,7 +23,10 @@ def tell(self):
23
23
return self .position
24
24
25
25
def flush (self ):
26
- self .handler .flush ()
26
+ # Note: Flushing is done elsewhere, in the main thread
27
+ # because `write()` is called in a background thread.
28
+ # self.handler.flush()
29
+ pass
27
30
28
31
29
32
def make_writer (handler , archive_format = "zip" ):
@@ -89,17 +92,26 @@ def get(self, archive_path, include_body=False):
89
92
self .set_header ('content-disposition' ,
90
93
'attachment; filename={}' .format (archive_filename ))
91
94
92
- task = asyncio .ensure_future (self .archive_and_download (archive_path , archive_format , archive_token ))
95
+ self .flush_cb = ioloop .PeriodicCallback (self .flush , 100 )
96
+ self .flush_cb .start ()
93
97
98
+ args = (archive_path , archive_format , archive_token )
94
99
try :
95
- yield task
96
- except (asyncio .CancelledError , iostream .StreamClosedError ):
97
- task .cancel ()
100
+ yield ioloop .IOLoop .current ().run_in_executor (None , self .archive_and_download , * args )
101
+
102
+ ## FIXME: Catching the exception does not work.
103
+ except Exception :
98
104
self .log .info ('Download canceled.' )
105
+
99
106
else :
107
+ self .flush ()
108
+ self .set_cookie ("archiveToken" , archive_token )
109
+
100
110
self .log .info ('Finished downloading {}.' .format (archive_filename ))
101
111
102
- @gen .coroutine
112
+ self .flush_cb .stop ()
113
+ self .finish ()
114
+
103
115
def archive_and_download (self , archive_path , archive_format , archive_token ):
104
116
105
117
with make_writer (self , archive_format ) as archive :
@@ -109,10 +121,6 @@ def archive_and_download(self, archive_path, archive_format, archive_token):
109
121
file_name = os .path .join (root , file_ )
110
122
self .log .debug ("{}\n " .format (file_name ))
111
123
archive .add (file_name , os .path .join (root [prefix :], file_ ))
112
- yield self .flush ()
113
-
114
- self .set_cookie ("archiveToken" , archive_token )
115
- self .finish ()
116
124
117
125
118
126
class ExtractArchiveHandler (IPythonHandler ):
0 commit comments