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:

committed by
Aleksander Morgado

parent
e685ce9ce5
commit
69926d9335
@@ -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()
|
||||
|
Reference in New Issue
Block a user