Skip to content

Commit 3604a93

Browse files
committed
Run archive_and_download in an executor to make it really not blocking.
1 parent f7addd7 commit 3604a93

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

jupyter_archive/handlers.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def tell(self):
2323
return self.position
2424

2525
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
2730

2831

2932
def make_writer(handler, archive_format="zip"):
@@ -89,17 +92,26 @@ def get(self, archive_path, include_body=False):
8992
self.set_header('content-disposition',
9093
'attachment; filename={}'.format(archive_filename))
9194

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()
9397

98+
args = (archive_path, archive_format, archive_token)
9499
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:
98104
self.log.info('Download canceled.')
105+
99106
else:
107+
self.flush()
108+
self.set_cookie("archiveToken", archive_token)
109+
100110
self.log.info('Finished downloading {}.'.format(archive_filename))
101111

102-
@gen.coroutine
112+
self.flush_cb.stop()
113+
self.finish()
114+
103115
def archive_and_download(self, archive_path, archive_format, archive_token):
104116

105117
with make_writer(self, archive_format) as archive:
@@ -109,10 +121,6 @@ def archive_and_download(self, archive_path, archive_format, archive_token):
109121
file_name = os.path.join(root, file_)
110122
self.log.debug("{}\n".format(file_name))
111123
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()
116124

117125

118126
class ExtractArchiveHandler(IPythonHandler):

0 commit comments

Comments
 (0)