Skip to content

Instantly share code, notes, and snippets.

@se7enack
Last active October 20, 2024 18:51
Show Gist options
  • Save se7enack/c9bb579137268f06537e80776d284cc6 to your computer and use it in GitHub Desktop.
Save se7enack/c9bb579137268f06537e80776d284cc6 to your computer and use it in GitHub Desktop.
Keyboard Interrupt and threading
#!/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