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.
This commit is contained in:
Yegor Yefremov
2021-02-15 17:53:39 +01:00
committed by Aleksander Morgado
parent e685ce9ce5
commit 69926d9335

View File

@@ -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()