Last active
October 20, 2024 18:51
-
-
Save se7enack/c9bb579137268f06537e80776d284cc6 to your computer and use it in GitHub Desktop.
Keyboard Interrupt and threading
This file contains hidden or 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
#!/usr/bin/env python3 | |
import time | |
import threading as thr | |
count = 5 | |
def sleeping(): | |
countdown = count | |
print("Countdown:", end=" ") | |
for i in range(count): | |
if go: | |
print(countdown, end=" ") | |
time.sleep(1) | |
countdown = countdown - 1 | |
print("") | |
def start(): | |
thread = thr.Thread(target=sleeping) | |
thread.start() | |
print("Main thread work happening at the same time") | |
time.sleep(2) | |
thread.join() | |
print("stuff to do after countdown thread finishes") | |
if __name__ == '__main__': | |
try: | |
go = True | |
start() | |
except KeyboardInterrupt: | |
go = False | |
print('\n\n\n -------------\n User Canceled\n -------------\n\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment