I have one module which uses python "threading" for concurrency, and "signal" for shutdown hook:
signal.signal(signal.SIGINT, self.shutdownhook)
I have another module which uses dbus and gobject
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
....
GObject.threads_init()
mainloop = GObject.MainLoop()
mainloop.run()
When I run them seperately, they both operate as expected and ctrl+c
causes termination via "KeyboardInterrupt".
However, when I run them together the mainloop terminates but the shutdown hook is never called - the process does not terminate without kill -9 pid
.
Can someone please explain why this occurs, and how best to integrate the two models
Here is a working example which highlights my problem. I cannot exit the program with just a CTRL+C and the shutdown hook is not called in this case either.
import threading
import signal
import sys
from gi.repository import GObject
def runMainloop():
print('running mainloop')
mainloop.run()
def shutdown():
print('shutdown')
def readInput():
print('readInput')
print(sys.stdin.readline())
if __name__ == '__main__':
signal.signal(signal.SIGINT, shutdown)
signal.signal(signal.SIGTERM, shutdown)
GObject.threads_init()
mainloop = GObject.MainLoop()
mainloopThread = threading.Thread(name='mainloop', target=runMainloop)
mainloopThread.setDaemon(True)
mainloopThread.start()
print('started')
inputThread = threading.Thread(name='input', target=readInput)
inputThread.start()
print('started input')