Stopping Python Script

I have a script that gets keyboard input. I want the script to be terminated when the stop button is pressed. I am using simulation context’s is_playing() to enter my stop_script() function, but I do not know what to do inside the stop_script() function, so that the keyboard subscriptions and the script is terminated.