diff options
author | Yegor Yefremov <yegorslists@googlemail.com> | 2021-02-15 17:53:39 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@aleksander.es> | 2021-02-15 21:10:34 +0100 |
commit | 69926d93357cc0ed7280586abee1e6871e0894cc (patch) | |
tree | 718c69dc020917032aea4e0c3d3889a877733444 | |
parent | e685ce9ce51f5f1e18ab3f42106715fd659ed2c6 (diff) |
examples: modem-watcher: get rid of global variables
Move the code into the main() routine and pass main_loop as
a parameter to the signal handler.
-rwxr-xr-x | examples/modem-watcher-python/modem-watcher-python | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/examples/modem-watcher-python/modem-watcher-python b/examples/modem-watcher-python/modem-watcher-python index 4884beef..f7dd1fc6 100755 --- a/examples/modem-watcher-python/modem-watcher-python +++ b/examples/modem-watcher-python/modem-watcher-python @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- # # This program is free software; you can redistribute it and/or modify it under @@ -24,21 +24,28 @@ from gi.repository import GLib import ModemWatcher -main_loop = None -watcher = None -def signal_handler(data): - main_loop.quit() +def signal_handler(loop): + """SIGHUP and SIGINT handler.""" + loop.quit() -if __name__ == "__main__": + +def main(): + """Main routine.""" # Create modem watcher - watcher = ModemWatcher.ModemWatcher() + ModemWatcher.ModemWatcher() # Main loop main_loop = GLib.MainLoop() - GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGHUP, signal_handler, None) - GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGTERM, signal_handler, None) + GLib.unix_signal_add( + GLib.PRIORITY_HIGH, signal.SIGHUP, signal_handler, main_loop) + GLib.unix_signal_add( + GLib.PRIORITY_HIGH, signal.SIGTERM, signal_handler, main_loop) try: main_loop.run() except KeyboardInterrupt: pass + + +if __name__ == "__main__": + main() |