Flavors of `exit` in Python

Posted by BugDiver on May 09, 2018

Few days ago I was writing some python code. The task quite simple, I had to stop a program if it is taking more time than expected.

"That,s pretty simple. I'll user Timer"

That's Exactly what I thouhgt. So I went ahead and wrote my code directly in the codebase instead of verifying it or doing some spike about timers.

I executed the application and it didn't time out.

Here is a sample code which kinda replicate what I tried

400: Invalid request

If you run this script you will see that the output contains both "watchdog expired. exiting!" and Completed!!".

Now the quistion is how the program continued after sys.exit.

So I tried to use a different method of exit.

400: Invalid request

But got no luck and the result was same. Output was still having both both "watchdog expired. exiting!" and Completed!!".

Now I started looking for some solutions on stackoverflow. Most of the answers were suggesting to use exit or sys.exit

Somehow I found out about another exit method inside the os package. Though it claimed to be a private method (with a leading _) I used it.

400: Invalid request

And guess what?

Horray...

It did exactly what I wanted.

Console just had "watchdog expired. exiting!". I put this in my actual codebase, ran the application and it worked.

These are couple of links which explains the difference between all the exit methods -