File tree Expand file tree Collapse file tree 1 file changed +24
-10
lines changed Expand file tree Collapse file tree 1 file changed +24
-10
lines changed Original file line number Diff line number Diff line change 15
15
16
16
from matplotlib .externals import six
17
17
18
- import datetime
19
18
import errno
20
19
import json
21
20
import os
22
21
import random
23
22
import sys
23
+ import signal
24
24
import socket
25
25
import threading
26
+ from contextlib import contextmanager
26
27
27
28
try :
28
29
import tornado
@@ -323,26 +324,39 @@ def start(cls):
323
324
if cls .started :
324
325
return
325
326
326
- # Set the flag to True *before* blocking on IOLoop.instance().start()
327
- cls .started = True
328
-
329
327
"""
330
328
IOLoop.running() was removed as of Tornado 2.4; see for example
331
329
https://groups.google.com/forum/#!topic/python-tornado/QLMzkpQBGOY
332
330
Thus there is no correct way to check if the loop has already been
333
331
launched. We may end up with two concurrently running loops in that
334
332
unlucky case with all the expected consequences.
335
333
"""
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 ()
341
338
print ("Server is stopped" )
342
339
sys .stdout .flush ()
343
- finally :
344
340
cls .started = False
345
341
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
+
346
360
347
361
def ipython_inline_display (figure ):
348
362
import tornado .template
You can’t perform that action at this time.
0 commit comments