File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1515
1616from matplotlib .externals import six
1717
18- import datetime
1918import errno
2019import json
2120import os
2221import random
2322import sys
23+ import signal
2424import socket
2525import threading
26+ from contextlib import contextmanager
2627
2728try :
2829 import tornado
@@ -323,26 +324,39 @@ def start(cls):
323324 if cls .started :
324325 return
325326
326- # Set the flag to True *before* blocking on IOLoop.instance().start()
327- cls .started = True
328-
329327 """
330328 IOLoop.running() was removed as of Tornado 2.4; see for example
331329 https://groups.google.com/forum/#!topic/python-tornado/QLMzkpQBGOY
332330 Thus there is no correct way to check if the loop has already been
333331 launched. We may end up with two concurrently running loops in that
334332 unlucky case with all the expected consequences.
335333 """
336- print ("Press Ctrl+C to stop WebAgg server" )
337- sys .stdout .flush ()
338- try :
339- tornado .ioloop .IOLoop .instance ().start ()
340- except KeyboardInterrupt :
334+ ioloop = tornado .ioloop .IOLoop .instance ()
335+
336+ def shutdown ():
337+ ioloop .stop ()
341338 print ("Server is stopped" )
342339 sys .stdout .flush ()
343- finally :
344340 cls .started = False
345341
342+ @contextmanager
343+ def catch_sigint ():
344+ old_handler = signal .signal (
345+ signal .SIGINT ,
346+ lambda sig , frame : ioloop .add_callback_from_signal (shutdown ))
347+ try :
348+ yield
349+ finally :
350+ signal .signal (signal .SIGINT , old_handler )
351+
352+ # Set the flag to True *before* blocking on ioloop.start()
353+ cls .started = True
354+
355+ print ("Press Ctrl+C to stop WebAgg server" )
356+ sys .stdout .flush ()
357+ with catch_sigint ():
358+ ioloop .start ()
359+
346360
347361def ipython_inline_display (figure ):
348362 import tornado .template
You can’t perform that action at this time.
0 commit comments