Commit 5476b345352eededeb8b85d767c143dcf66eff68
1 parent
2d620b8a
Exists in
master
and in
1 other branch
- Ask yes/no to confirm stopping the server
Showing
2 changed files
with
20 additions
and
5 deletions
Show diff stats
serve.py
... | ... | @@ -9,6 +9,7 @@ import logging.config |
9 | 9 | import argparse |
10 | 10 | from concurrent.futures import ThreadPoolExecutor |
11 | 11 | import mimetypes |
12 | +import signal | |
12 | 13 | |
13 | 14 | # user installed libraries |
14 | 15 | import tornado.ioloop |
... | ... | @@ -285,6 +286,16 @@ class QuestionHandler(BaseHandler): |
285 | 286 | } |
286 | 287 | }) |
287 | 288 | |
289 | + | |
290 | +# ------------------------------------------------------------------------- | |
291 | +def signal_handler(signal, frame): | |
292 | + r = input(' --> Stop webserver? (yes/no) ') | |
293 | + if r in ('yes', 'YES'): | |
294 | + tornado.ioloop.IOLoop.current().stop() | |
295 | + logging.critical('Webserver stopped.') | |
296 | + sys.exit(0) | |
297 | + | |
298 | + | |
288 | 299 | # ------------------------------------------------------------------------- |
289 | 300 | # Tornado web server |
290 | 301 | # ---------------------------------------------------------------------------- |
... | ... | @@ -339,13 +350,17 @@ def main(): |
339 | 350 | http_server.listen(8443) |
340 | 351 | |
341 | 352 | # --- run webserver |
342 | - logging.info('Webserver running...') | |
353 | + logging.info('Webserver running... (Ctrl-C to stop)') | |
354 | + signal.signal(signal.SIGINT, signal_handler) | |
355 | + | |
356 | + | |
343 | 357 | try: |
344 | 358 | tornado.ioloop.IOLoop.current().start() # running... |
345 | - except KeyboardInterrupt: | |
359 | + except Exception: | |
360 | + logging.critical('Webserver stopped.') | |
346 | 361 | tornado.ioloop.IOLoop.current().stop() |
362 | + raise | |
347 | 363 | |
348 | - logging.critical('Webserver stopped.') | |
349 | 364 | |
350 | 365 | # ---------------------------------------------------------------------------- |
351 | 366 | if __name__ == "__main__": | ... | ... |
tools.py
... | ... | @@ -16,11 +16,11 @@ from pygments.formatters import HtmlFormatter |
16 | 16 | logger = logging.getLogger(__name__) |
17 | 17 | |
18 | 18 | |
19 | -# --------------------------------------------------------------------------- | |
19 | +# ------------------------------------------------------------------------- | |
20 | 20 | # Markdown to HTML renderer with support for LaTeX equations |
21 | 21 | # Inline math: $x$ |
22 | 22 | # Block math: $$x$$ or \begin{equation}x\end{equation} |
23 | -# --------------------------------------------------------------------------- | |
23 | +# ------------------------------------------------------------------------- | |
24 | 24 | class MathBlockGrammar(mistune.BlockGrammar): |
25 | 25 | block_math = re.compile(r"^\$\$(.*?)\$\$", re.DOTALL) |
26 | 26 | latex_environment = re.compile(r"^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}", re.DOTALL) | ... | ... |