I have a discord bot that uses an sqlite database to store files. I use a cache in runtime but have an autosave loop to keep saving the data every so often
this works, but when the script terminates (i press stop in vs code, or on my python server, or close my terminal window) the program does not get a chance to do a last commit to the database and all the data that was edited between the last autosave and the exit is lost.
how can i prevent this?
#saving data on exit
1 messages · Page 1 of 1 (latest)
Use a shutdown handler to commit before the program exits.
import sys
def shutdown_handler(signum, frame):
print("Shutting down, committing DB...")
conn.commit() # your sqlite connection
conn.close()
sys.exit(0)
signal.signal(signal.SIGINT, shutdown_handler) # Ctrl+C / stop
signal.signal(signal.SIGTERM, shutdown_handler) # kill / service stop ```