I am learning how to use signals. I created a small program that connects to a database and prints the results. I am leaving the database connection open. I am sending a SIGTERM to end the program. That works. But when I try to add my database disconnect object it fails. I made db global. Any help or explanation of what I am doing incorrectly. My plan to to clean up socket connections and others to shut the program down gracefully.
global db
def term_handler(signum,frame):
print('In term handler')
db.disconnect()
#more clean up to come.
sys.exit()
def main():
db = connection
results = db.select('query here')
print(results.fetchall())
#db.disconnect()
# The above disconnect works when in main. but fails when in term_handler
signal.signal(signal.SIGTERM,term_handler)
while True:
print('yes')
sleep(2)
return None
if __name__ == "__main__":
main()