Quit() to recognise IDE stop button fuction!

Hi,

I’m using Python concurrent.future threads … in Pycharm.

I have a save/load project function for my program … no GUI.
I’m using the Pycharm square stop box to exit the program.

My Save function
I cannot safely write to disk, which threads are done/pending …
because there is a possibility the save file will corrupt if I push
the square stop box while (example - thread 5) is saving.

My Load function
I have created a workaround by backing up the save file …
and reloading it … if the written file was corrupted on shutdown.

QUESTION
If we could include a feature into quit() or exit() … that asks the user …
“Was python closed?” (by Pycharm or some other IDE) … then
if it was closed … a final call is made to quit() … to run some final code
the user has written.

is this possible?

The built-in atexit module is used to run functions when Python is exiting.

The built-in signals module is used to run functions when signals such as “quit” are received from the OS.

atexit handles exits … But this function does not catch when another IDE such as Pycharm … performs a stop button exit.

The idea im suggesting is like a background process that recognises when any IDE has shut down python remotely … THEN executes the atexit code you have written.

If you are trying to solve a process issue using another process you are definitely on the wrong track.

In linux, I bet the red square button first sends SIGINT (which usually turns into KeyboardInterrupt) and clicking it twice will send SIGTERM or SIGKILL. You can usually save your stuff no problem by using finally:, etc.

This is actually working. If I wrap the … write to disk … inside a try, finally statement … I can request the write to disk is re-attempted within the finally … thank you!