Created
June 28, 2022 21:40
-
-
Save hclivess/4972733b2ffec995c707ce42001b3963 to your computer and use it in GitHub Desktop.
process_termination.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import signal | |
import time | |
import readchar | |
def handler(signum, frame): | |
msg = "Ctrl-c was pressed. Do you really want to exit? y/n " | |
print(msg, end="", flush=True) | |
res = readchar.readchar() | |
if res == 'y': | |
print("") | |
exit(1) | |
else: | |
print("", end="\r", flush=True) | |
print(" " * len(msg), end="", flush=True) # clear the printed line | |
print(" ", end="\r", flush=True) | |
signal.signal(signal.SIGINT, handler) | |
count = 0 | |
while True: | |
print(f"{count}", end="\r", flush=True) | |
count += 1 | |
time.sleep(0.1) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment