Skip to content

Commit

Permalink
better shutdown of server, so we can reuse the port, see: tornadoweb/…
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenbreddels committed Nov 2, 2018
1 parent 4113c50 commit be0c634
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions nbconvert/preprocessors/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,15 @@ def callback(self):
for key, value in self.snapshot_dict.items():
if value['data'][MIME_TYPE_PNG] is None:
done = False

if done and not self.keep_running:
self.main_ioloop.stop()
self.stop_server()

# see https://github.com/tornadoweb/tornado/issues/2523
def stop_server(self):
self.http_server.stop()
self.main_ioloop.add_future(self.http_server.close_all_connections(),
lambda x: self.main_ioloop.stop())

def preprocess(self, nb, resources):
"""Serve the build directory with a webserver."""
Expand Down Expand Up @@ -194,12 +201,11 @@ def preprocess(self, nb, resources):
client=AsyncHTTPClient(),
)

# hook up tornado logging to our logger
# hook up tornado logger to our logger
log.app_log = self.log

http_server = httpserver.HTTPServer(app)
self.port = next(next_port)
http_server.listen(self.port, address=self.ip)
self.http_server = httpserver.HTTPServer(app)
self.http_server.listen(self.port, address=self.ip)
url = "http://%s:%i/%s" % (self.ip, self.port, filename)
print("Serving your slides at %s" % url)
print("Use Control-C to stop this server")
Expand All @@ -212,12 +218,10 @@ def preprocess(self, nb, resources):
self.main_ioloop.start()
except KeyboardInterrupt:
print("\nInterrupted")
http_server.stop()
self.http_server.stop()
# TODO: maybe we need to wait for this to finish
self.http_server.close_all_connections()
# nbformat.write(self.nb, input.replace('.html', '.ipynb'))
# import IPython
# IPython.embed()
# import pdb
# pdb.set_trace()
return nb, resources

def main(path):
Expand Down

0 comments on commit be0c634

Please sign in to comment.