def ignore_sigint(func):
 @wraps(func)
 def wrapped(*args, **kwargs):
  curr_thread = threading.currentThread()
  single_thread = ((threading.activeCount() == 1) and (curr_thread.getName() == 'MainThread'))
  class SigintHandler(object, ):
   def __init__(self):
    self.sigint_received = False
   def __call__(self, signum, frame):
    warnings.warn('KeyboardInterrupt ignored until {} is complete!'.format(func.__name__), AstropyUserWarning)
    self.sigint_received = True
  sigint_handler = SigintHandler()
  if single_thread:
   old_handler = signal.signal(signal.SIGINT, sigint_handler)
  try:
   func(*args, **kwargs)
  finally:
   if single_thread:
    if (old_handler is not None):
     signal.signal(signal.SIGINT, old_handler)
    else:
     signal.signal(signal.SIGINT, signal.SIG_DFL)
    if sigint_handler.sigint_received:
     raise KeyboardInterrupt
 return wrapped

def ignore_sigint ( func ) : @ wraps ( func ) def wrapped ( * args , ** kwargs ) : curr_thread = threading . currentThread ( ) single_thread = ( ( threading . activeCount ( ) == 1 ) and ( curr_thread . getName ( ) == 'MainThread' ) ) class SigintHandler ( object , ) : def __init__ ( self ) : self . sigint_received = False def __call__ ( self , signum , frame ) : warnings . warn ( 'KeyboardInterrupt ignored until {} is complete!' . format ( func . __name__ ) , AstropyUserWarning ) self . sigint_received = True sigint_handler = SigintHandler ( ) if single_thread : old_handler = signal . signal ( signal . SIGINT , sigint_handler ) try : func ( * args , ** kwargs ) finally : if single_thread : if ( old_handler is not None ) : signal . signal ( signal . SIGINT , old_handler ) else : signal . signal ( signal . SIGINT , signal . SIG_DFL ) if sigint_handler . sigint_received : raise KeyboardInterrupt return wrapped
this decorator registers a custom sigint handler to catch and ignore sigint until the wrapped function is completed .