Ask coding questions

← Back to all posts
Callback function before stopping the repl
ArgoPanoptes (0)

Is there a way to call a callback function before the repl is stopped when we click on "Stop" button. Eg. if I have a discord bot and I click on "Stop" button, I will be able to call a callback function that will send a message to alert the users that the bot is down.

I think it can be done if I stop the repl programmatically, but that requires more code and I also need to manage carefully multiprocessor repls and make the processes deamon to avoid orphan processes.

SixBeeps (5052)

There's no hook directly into the Run button, unfortunately.
You could give this a try, though I'm not entirely sure if pressing the stop button counts as a signal or not.

ArgoPanoptes (0)

@SixBeeps I tried it but it does not works. If i click on "Stop" it does not call the callback function. It does call it if send the kill signal CTRL+C. I added the sys.exit in the callback because without it the repl will not stop even after the kill signal is sent. In a multiprocess repl the processes have to be daemon or when the kill signal is sent to the parent, the parent should send it to the children, if not there could be orphan processes.