Exitting gracefully in Python

Hey there! I’m making a discord bot in Python and hosting it on Glitch.

I know Python isn’t officially supported by Glitch, yet I like to break the limits, I guess.
I’ve encountered one problem when I’m trying to use signal.signal(), Glitch just doesn’t care if I want to exit gracefully and my signal.signal() doesn’t run.

How can I exit gracefully in Python when Glitch wants to restart my app?
Thank you

Hi, I just tried and got the same result as you did.

Maybe either of these could be a workaround:

  • npm stop script? might need nodemon
  • a parallel node process that catches SIGTERM and sends a signal to the python app.

Hey there.

I don’t think the parallel thing would work, considering Glitch simply decides to stop all running scripts.
And what do you exactly mean by npm stop script?

Thanks!

Hey @Aimarekin, do you know how you’re running your project? I was able to build a simple flask app that exits gracefully using signal.signal:

def terminateProcess(signalNumber, frame):
  print "Goodbye"
  exit()

signal.signal(signal.SIGTERM, terminateProcess)

Interesting, I got this to work in python3 with flask too, although the print didn’t show in the log, it was able to write to a file before exit.

The other time it didn’t work maybe because I didn’t have a listener active.

Oh, turns out, it actually was running, but print() isn’t shown!

I didn’t realize the bot actually went offline when restarting meaning it was exitting gracefully because print() didn’t show up!
So yup, it does run, but the console doesn’t update or something.

I think the folks at Glitch should note this down.

Thanks!