我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用signal.SIG_IGN。
def start_service(self): """ start speaker training service. """ # prevent signal from propagating to child process handler = signal.getsignal(signal.SIGINT) signal.signal(signal.SIGINT, signal.SIG_IGN) if self.debug: self.sprecog.debug = True mp.log_to_stderr(logging.DEBUG) self.sprecog.speaker_name = self.speaker_name self.proc = mp.Process(name="watchdog", target=self.__run, args=(self.event,)) self.proc.setDaemon = False self.proc.start() # restore signal signal.signal(signal.SIGINT, handler)
def per_process_init(): # type: () -> None try: os.nice(19) except AttributeError: # nice is not available everywhere. pass except OSError: # When this program is already running on the nicest level (20) on OS X # it is not permitted to change the priority. pass # A keyboard interrupt disrupts the communication between a # Python script and its subprocesses when using multiprocessing. # The child can ignore SIGINT and is properly shut down # by a pool.terminate() call in case of a keyboard interrupt # or an early generator exit. signal.signal(signal.SIGINT, signal.SIG_IGN)
def __init__(self, task_name, manager, config, timer, base_dir, backup_dir, **kwargs): self.task_name = task_name self.manager = manager self.config = config self.timer = timer self.base_dir = base_dir self.backup_dir = backup_dir self.args = kwargs self.verbose = self.config.verbose self.runnning = False self.stopped = False self.completed = False self.exit_code = 255 self.thread_count = None self.cpu_count = cpu_count() self.compression_method = 'none' self.compression_supported = ['none'] self.timer_name = self.__class__.__name__ signal(SIGINT, SIG_IGN) signal(SIGTERM, self.close)
def testSecondInterrupt(self): # Can't use skipIf decorator because the signal handler may have # been changed after defining this method. if signal.getsignal(signal.SIGINT) == signal.SIG_IGN: self.skipTest("test requires SIGINT to not be ignored") result = unittest.TestResult() unittest.installHandler() unittest.registerResult(result) def test(result): pid = os.getpid() os.kill(pid, signal.SIGINT) result.breakCaught = True self.assertTrue(result.shouldStop) os.kill(pid, signal.SIGINT) self.fail("Second KeyboardInterrupt not raised") try: test(result) except KeyboardInterrupt: pass else: self.fail("Second KeyboardInterrupt not raised") self.assertTrue(result.breakCaught)
def testHandlerReplacedButCalled(self): # Can't use skipIf decorator because the signal handler may have # been changed after defining this method. if signal.getsignal(signal.SIGINT) == signal.SIG_IGN: self.skipTest("test requires SIGINT to not be ignored") # If our handler has been replaced (is no longer installed) but is # called by the *new* handler, then it isn't safe to delay the # SIGINT and we should immediately delegate to the default handler unittest.installHandler() handler = signal.getsignal(signal.SIGINT) def new_handler(frame, signum): handler(frame, signum) signal.signal(signal.SIGINT, new_handler) try: pid = os.getpid() os.kill(pid, signal.SIGINT) except KeyboardInterrupt: pass else: self.fail("replaced but delegated handler doesn't raise interrupt")
def __init__(self, default_handler): self.called = False self.original_handler = default_handler if isinstance(default_handler, int): if default_handler == signal.SIG_DFL: # Pretend it's signal.default_int_handler instead. default_handler = signal.default_int_handler elif default_handler == signal.SIG_IGN: # Not quite the same thing as SIG_IGN, but the closest we # can make it: do nothing. def default_handler(unused_signum, unused_frame): pass else: raise TypeError("expected SIGINT signal handler to be " "signal.SIG_IGN, signal.SIG_DFL, or a " "callable object") self.default_handler = default_handler
def _import_mp(): global Process, Queue, Pool, Event, Value, Array try: from multiprocessing import Manager, Process #prevent the server process created in the manager which holds Python #objects and allows other processes to manipulate them using proxies #to interrupt on SIGINT (keyboardinterrupt) so that the communication #channel between subprocesses and main process is still usable after #ctrl+C is received in the main process. old=signal.signal(signal.SIGINT, signal.SIG_IGN) m = Manager() #reset it back so main process will receive a KeyboardInterrupt #exception on ctrl+c signal.signal(signal.SIGINT, old) Queue, Pool, Event, Value, Array = ( m.Queue, m.Pool, m.Event, m.Value, m.Array ) except ImportError: warn("multiprocessing module is not available, multiprocess plugin " "cannot be used", RuntimeWarning)
def testSecondInterrupt(self): # Can't use skipIf decorator because the signal handler may have # been changed after defining this method. if signal.getsignal(signal.SIGINT) == signal.SIG_IGN: self.skipTest("test requires SIGINT to not be ignored") result = unittest2.TestResult() unittest2.installHandler() unittest2.registerResult(result) def test(result): pid = os.getpid() os.kill(pid, signal.SIGINT) result.breakCaught = True self.assertTrue(result.shouldStop) os.kill(pid, signal.SIGINT) self.fail("Second KeyboardInterrupt not raised") try: test(result) except KeyboardInterrupt: pass else: self.fail("Second KeyboardInterrupt not raised") self.assertTrue(result.breakCaught)
def testHandlerReplacedButCalled(self): # Can't use skipIf decorator because the signal handler may have # been changed after defining this method. if signal.getsignal(signal.SIGINT) == signal.SIG_IGN: self.skipTest("test requires SIGINT to not be ignored") # If our handler has been replaced (is no longer installed) but is # called by the *new* handler, then it isn't safe to delay the # SIGINT and we should immediately delegate to the default handler unittest2.installHandler() handler = signal.getsignal(signal.SIGINT) def new_handler(frame, signum): handler(frame, signum) signal.signal(signal.SIGINT, new_handler) try: pid = os.getpid() os.kill(pid, signal.SIGINT) except KeyboardInterrupt: pass else: self.fail("replaced but delegated handler doesn't raise interrupt")
def init_worker(num_instances, kernel_h, kernel_w, pad, stride, indices, pdfs): global g_num_instances g_num_instances = num_instances global g_kernel_h g_kernel_h = kernel_h global g_kernel_w g_kernel_w = kernel_w global g_pad g_pad = pad global g_stride g_stride = stride global g_indices g_indices = indices global g_pdfs g_pdfs = pdfs signal.signal(signal.SIGINT, signal.SIG_IGN) np.random.seed(None)
def save(self): start = time.time() print "Saving the model..." # ignore keyboard interrupt while saving s = signal.signal(signal.SIGINT, signal.SIG_IGN) numpy.savez(self.state['prefix']+'timing.npz', **self.timings) if self.state['overwrite']: self.model.save(self.state['prefix']+'model.npz') else: self.model.save(self.state['prefix'] + 'model%d.npz' % self.save_iter) cPickle.dump(self.state, open(self.state['prefix']+'state.pkl', 'w')) self.save_iter += 1 signal.signal(signal.SIGINT, s) print "Model saved, took {}".format(time.time() - start) # FIXME
def _make_signal_handler(self, target): """ Make the signal handler for a specified target object. :param target: A specification of the target for the handler; see below. :return: The value for use by `signal.signal()`. If `target` is ``None``, return ``signal.SIG_IGN``. If `target` is a text string, return the attribute of this instance named by that string. Otherwise, return `target` itself. """ if target is None: result = signal.SIG_IGN elif isinstance(target, unicode): name = target result = getattr(self, name) else: result = target return result
def terminate(self): if self.terminated: return self.terminated = True def flushevents(): while True: try: event = self.event_queue.get(block=False) except (Empty, IOError): break if isinstance(event, logging.LogRecord): logger.handle(event) signal.signal(signal.SIGINT, signal.SIG_IGN) self.procserver.stop() while self.procserver.is_alive(): flushevents() self.procserver.join(0.1) self.ui_channel.close() self.event_queue.close() self.event_queue.setexit() # Wrap Queue to provide API which isn't server implementation specific
def __init__(self, bot=None, machines=None, **kwargs): """ Implements a sequence of multiple machines :param machines: the sequence of machines to be ran :type machines: list of Machine """ self.bot = bot self.machines = machines self.lock = Lock() # prevent Manager() process to be interrupted handler = signal.signal(signal.SIGINT, signal.SIG_IGN) self.mutables = Manager().dict() # restore current handler for the rest of the program signal.signal(signal.SIGINT, handler) self.on_init(**kwargs)
def __init__(self, settings=None, filter=None): """ Stores settings across multiple independent processing units :param settings: the set of variables managed in this context :type settings: dict :param filter: a function to interpret values on check() :type filter: callable """ # prevent Manager() process to be interrupted handler = signal.signal(signal.SIGINT, signal.SIG_IGN) self.lock = Lock() self.values = Manager().dict() # restore current handler for the rest of the program signal.signal(signal.SIGINT, handler) self.filter = filter if filter else self._filter if settings: self.apply(settings)
def _save_model(self, bleu_score): if self._is_valid_to_save(bleu_score): model = ModelInfo(bleu_score, self.config['saveto']) # Manage n-best model list first if len(self.best_models) >= self.track_n_models: old_model = self.best_models[0] if old_model.path and os.path.isfile(old_model.path): logger.info("Deleting old model %s" % old_model.path) os.remove(old_model.path) self.best_models.remove(old_model) self.best_models.append(model) self.best_models.sort(key=operator.attrgetter('bleu_score')) # Save the model here s = signal.signal(signal.SIGINT, signal.SIG_IGN) logger.info("Saving new model {}".format(model.path)) numpy.savez( model.path, **self.main_loop.model.get_parameter_dict()) numpy.savez( os.path.join(self.config['saveto'], 'val_bleu_scores.npz'), bleu_scores=self.val_bleu_curve) signal.signal(signal.SIGINT, s)
def __init__(self): # process command line options and load config files self._config = Config() self._threads = [] self._exiting = False self._reload = False # signal handling for sig, action in ( (signal.SIGINT, self.shutdown), (signal.SIGQUIT, self.shutdown), (signal.SIGTERM, self.shutdown), (signal.SIGHUP, lambda s, f: setattr(self, '_reload', True)), (signal.SIGPIPE, signal.SIG_IGN), ): try: signal.signal(sig, action) except AttributeError: pass log.trace(self._config)
def __init__(self, parallel): self.continuation_prompt = self.prompt self.parallel = parallel width, height = get_terminal_size() or MIN_TERM_SIZE if any(map((lambda s: s[0] < s[1]), zip((height, width), MIN_TERM_SIZE))): stdout.write("\x1b[8;{rows};{cols}t".format(rows=max(MIN_TERM_SIZE[0], height), cols=max(MIN_TERM_SIZE[1], width))) if self.parallel: processes = cpu_count() self.__last_tasklist = None self.tasklist = {} self.pool = Pool(processes, lambda: signal(SIGINT, SIG_IGN)) atexit.register(self.graceful_exit) self.reexec = ['status'] self.__bind_commands() super(FrameworkConsole, self).__init__() self.do_loglevel('info') self.__start_docserver() self.do_clear('')
def run(): global start, stop, count signal.signal(signal.SIGINT, signal.SIG_IGN) start = time.time() with open('results.txt', 'w') as output: with multiprocessing.Pool(processes=24) as pool: signal.signal(signal.SIGINT, handler=handle_signal) for domain in open('domains.txt'): domain = domain.strip() pool.apply_async(check_domain, args=(domain,), callback=create_callback(domain, output)) if stop: pool.terminate() break pool.close() pool.join()
def save(model, timings, train_iterator, post_fix = ''): print "Saving the model..." # ignore keyboard interrupt while saving start = time.time() s = signal.signal(signal.SIGINT, signal.SIG_IGN) model.state['train_iterator_offset'] = train_iterator.get_offset() + 1 model.state['train_iterator_reshuffle_count'] = train_iterator.get_reshuffle_count() model.save(model.state['save_dir'] + '/' + model.state['run_id'] + "_" + model.state['prefix'] + post_fix + 'model.npz') cPickle.dump(model.state, open(model.state['save_dir'] + '/' + model.state['run_id'] + "_" + model.state['prefix'] + post_fix + 'state.pkl', 'w')) numpy.savez(model.state['save_dir'] + '/' + model.state['run_id'] + "_" + model.state['prefix'] + post_fix + 'timing.npz', **timings) signal.signal(signal.SIGINT, s) print "Model saved, took {}".format(time.time() - start)
def napalm_logs_engine(): if '' in sys.path: sys.path.remove('') # Temporarily will forward the log entries to the screen # After reading the config and all good, will write into the right # log file. screen_logger = logging.StreamHandler(sys.stdout) screen_logger.setFormatter(logging.Formatter(defaults.LOG_FORMAT)) log.addHandler(screen_logger) nlop = NLOptionParser() config = nlop.parse(log, screen_logger) # Ignore SIGINT whilst starting child processes so they inherit the ignore signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN) nl = napalm_logs.NapalmLogs(**config) nl.start_engine() # Set SIGINT to _exit_gracefully so we can close everything down gracefully signal.signal(signal.SIGINT, _exit_gracefully) signal.signal(signal.SIGTERM, _exit_gracefully) # Keep this function running until we receive instruction to terminate while _up is True and nl.up is True: time.sleep(1) nl.stop_engine()
def init_worker(): signal.signal(signal.SIGINT, signal.SIG_IGN) np.random.seed(None) # Utilities for loading and saving images to/from numpy arrays
def __init__(self, *args, **kwargs): Resource.__init__(self, *args, **kwargs) # Ignore normal termination signals to ensure that the process is # orphaned when the parent is terminated; start_component() normally # establishes handlers for these before creating the component instance signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN) signal.signal(signal.SIGQUIT, signal.SIG_IGN)
def __exit_handler(signum, frame): # Raise SystemExit - but only the first time we get a signal signal.signal(signal.SIGINT, signal.SIG_IGN) signal.signal(signal.SIGQUIT, signal.SIG_IGN) signal.signal(signal.SIGTERM, signal.SIG_IGN) raise SystemExit # # Add required features to a python service instance #
def download_chunks(self, max_workers=5): print('Will now download chunks.') original_sigint_handler = signal.signal(signal.SIGINT, signal.SIG_IGN) executor = Pool(max_workers) signal.signal(signal.SIGINT, original_sigint_handler) try: r = executor.map_async(self.get, self.urls) result = list(r.get(43200)) DownloadResultProcessor.process_and_print(result) except KeyboardInterrupt: executor.terminate() else: executor.close() executor.join()
def do(self, which_callback, *args): iterations_done = self.main_loop.status['iterations_done'] if self.burnin <= iterations_done: # Save the model here iterations_done = self.main_loop.status['iterations_done'] filename = os.path.join( self.saveto, 'params_iter{}.npz'.format(iterations_done)) s = signal.signal(signal.SIGINT, signal.SIG_IGN) logger.info(" Incremental dump {}".format(filename)) params_to_save = [] for cg_name in self.main_loop.models.keys(): params_to_save.append( self.main_loop.models[cg_name].get_param_values()) params_to_save = merge(params_to_save) secure_numpy_save(params_to_save, filename) if self.save_iter_state: filename_is = os.path.join( self.saveto, 'iterations_state_iter{}.pkl'.format(iterations_done)) logger.info(" Incremental dump {}".format(filename_is)) secure_pickle_dump(self.main_loop.iteration_state, filename_is) if self.save_log: filename_log = os.path.join( self.saveto, 'log_iter{}'.format(iterations_done)) logger.info(" Incremental dump {}".format(filename_log)) secure_pickle_dump(self.main_loop.log, filename_log) signal.signal(signal.SIGINT, s)
def _save_model(self, bleu_score): if self._is_valid_to_save(bleu_score): model = ModelInfo( bleu_score, self.saveto, self.enc_id, self.dec_id) # Manage n-best model list first if len(self.best_models) >= self.track_n_models: old_model = self.best_models[0] if old_model.path and os.path.isfile(old_model.path): logger.info("Deleting old model %s" % old_model.path) os.remove(old_model.path) self.best_models.remove(old_model) self.best_models.append(model) self.best_models.sort(key=operator.attrgetter('bleu_score')) # Save the model here s = signal.signal(signal.SIGINT, signal.SIG_IGN) logger.info("Saving new model {}".format(model.path)) params_to_save = [] for cg_name in self.main_loop.models.keys(): params_to_save.append( self.main_loop.models[cg_name].get_param_values()) params_to_save = merge(params_to_save) self._save_params(model, params_to_save) self._save_bleu_scores() signal.signal(signal.SIGINT, s)
def non_interruptable(f): @wraps(f) def wrapped(*args, **kwargs): s = signal.signal(signal.SIGINT, signal.SIG_IGN) r = f(*args, **kwargs) signal.signal(signal.SIGINT, s) return r return wrapped
def set_signal_handlers(self): signal.signal(signal.SIGTERM, signal.SIG_DFL) signal.signal(signal.SIGUSR1, signal.SIG_IGN)