1

Basically I am writing a script that can be stopped and resumed at any time. So if the user uses, say PyCharm console to execute the program, he can just click on the stop button whenever he wants.

Now, I need to save some variables and let an ongoing function finish before terminating. What functions do I use for this?

I have already tried atexit.register() to no avail. Also, how do I make sure that an ongoing function is completed before the program can exit?

feetwet
  • 3,248
  • 7
  • 46
  • 84

2 Answers2

1

Solved it using a really bad workaround. I used all functions that are related to exit in Python, including SIG* functions, but uniquely, I did not find a way to catch the exit signal when Python program is being stopped by pressing the "Stop" button in PyCharm application. Finally got a workaround by using tkinter to open an empty window, with my program running in a background thread, and used that to close/stop program execution. Works wonderfully, and catches the SIG* signal as well as executing atexit . Anyways massive thanks to @scrineym as the link really gave a lot of useful information that did help me in development of the final version.

0

It looks like you might want to catch a signal.

When a program is told to stop a signal is sent to the process from the OS, you can then catch them and do cleanup before exit. There are many diffferent signals , for xample when you press CTRL+C a SIGINT signal is sent by the OS to stop your process, but there are many others.

See here : How do I capture SIGINT in Python?

and here for the signal library: https://docs.python.org/2/library/signal.html

Community
  • 1
  • 1
scrineym
  • 759
  • 1
  • 6
  • 28