我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用gi.repository.GLib.unix_signal_add()。
def InitSignal(gui): def signal_action(signal): if signal.value is signal.SIGINT.value: print("\r", end="") logging.debug("Caught signal SIGINT(2)") gui.stop() def handler(*args): signal_action(args[0]) def idle_handler(*args): GLib.idle_add(signal_action, priority=GLib.PRIORITY_HIGH) def install_glib_handler(sig): GLib.unix_signal_add(GLib.PRIORITY_HIGH, sig, handler, sig) SIGS = [getattr(signal, s, None) for s in "SIGINT".split()] for sig in filter(None, SIGS): signal.signal(sig, idle_handler) GLib.idle_add(install_glib_handler, sig, priority=GLib.PRIORITY_HIGH)
def setup_signals(self, signals, handler): """ This is a workaround to signal.signal(signal, handler) which does not work with a GLib.MainLoop() for some reason. Thanks to: http://stackoverflow.com/a/26457317/5433146 args: signals (list of signal.SIG... signals): the signals to connect to handler (function): function to be executed on these signals """ def install_glib_handler(sig): # add a unix signal handler GLib.unix_signal_add( GLib.PRIORITY_HIGH, sig, # for the given signal handler, # on this signal, run this function sig # with this argument ) for sig in signals: # loop over all signals GLib.idle_add( # 'execute' install_glib_handler, sig, # add a handler for this signal priority = GLib.PRIORITY_HIGH ) # set the config
def setup_signals(self, signals, handler): """ This is a workaround to signal.signal(signal, handler) which does not work with a GLib.MainLoop() for some reason. Thanks to: http://stackoverflow.com/a/26457317/5433146 args: signals (list of signal.SIG... signals): the signals to connect to handler (function): function to be executed on these signals """ def install_glib_handler(sig): # add a unix signal handler GLib.unix_signal_add( GLib.PRIORITY_HIGH, sig, # for the given signal handler, # on this signal, run this function sig # with this argument ) for sig in signals: # loop over all signals GLib.idle_add( # 'execute' install_glib_handler, sig, # add a handler for this signal priority = GLib.PRIORITY_HIGH ) # build the gui
def setup_signals(self, signals, handler): """ This is a workaround to signal.signal(signal, handler) which does not work with a GLib.MainLoop() for some reason. Thanks to: http://stackoverflow.com/a/26457317/5433146 args: signals (list of signal.SIG... signals): the signals to connect to handler (function): function to be executed on these signals """ def install_glib_handler(sig): # add a unix signal handler GLib.unix_signal_add( GLib.PRIORITY_HIGH, sig, # for the given signal handler, # on this signal, run this function sig # with this argument ) for sig in signals: # loop over all signals GLib.idle_add( # 'execute' install_glib_handler, sig, # add a handler for this signal priority = GLib.PRIORITY_HIGH ) # get an object from the builder
def main(): utils.parse_args(include_name=False) loop = GLib.MainLoop() GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGINT, signal_cb) GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGTERM, signal_cb) monitor = Gio.VolumeMonitor.get() store = simple_store.Producer(prefix=SUBSCRIPTIONS_SOMA, base=defaults.ENDLESS_NDN_CACHE_PATH, cost=defaults.RouteCost.USB) store.start() for mount in monitor.get_mounts(): mount_added_cb(monitor, mount, store) monitor.connect("mount-added", mount_added_cb, store) monitor.connect("mount-removed", mount_removed_cb, store) maybe_time_out() loop.run()
def SignalHandler(app): def signal_action(signal): if signal is 1: print("Caught signal SIGHUP(1)") elif signal is 2: print("Caught signal SIGINT(2)") elif signal is 15: print("Caught signal SIGTERM(15)") app.exit_gracefully() def handler(*args): # Activate GLib signal handler signal_action(args[0]) def idle_handler(*args): # Activate python signal handler GLib.idle_add(signal_action, priority=GLib.PRIORITY_HIGH) def install_glib_handler(sig): unix_signal_add = None if hasattr(GLib, "unix_signal_add"): unix_signal_add = GLib.unix_signal_add elif hasattr(GLib, "unix_signal_add_full"): unix_signal_add = GLib.unix_signal_add_full if unix_signal_add: # Register GLib signal handler unix_signal_add(GLib.PRIORITY_HIGH, sig, handler, sig) else: print("WARNING: Can't install GLib signal handler, too old gi.") SIGS = [getattr(signal, s, None) for s in "SIGINT SIGTERM SIGHUP".split()] for sig in filter(None, SIGS): # Register Python signal handler signal.signal(sig, idle_handler) GLib.idle_add(install_glib_handler, sig, priority=GLib.PRIORITY_HIGH)
def install_glib_handler(sig): unix_signal_add = None if hasattr(GLib, "unix_signal_add"): unix_signal_add = GLib.unix_signal_add elif hasattr(GLib, "unix_signal_add_full"): unix_signal_add = GLib.unix_signal_add_full if unix_signal_add: unix_signal_add(GLib.PRIORITY_HIGH, sig, handler, sig) else: print("Can't user Glib signal handler")