Skip to content

Instantly share code, notes, and snippets.

@sunjeet-khokhar sunjeet-khokhar/interrupt.py Secret
Last active Apr 14, 2018

Embed
What would you like to do?
try:
start_time = datetime.now()
#do something endlessly
while True:
print(datetime.now())
time.sleep(1)
# until interrupted by the keyboard
except KeyboardInterrupt:
#behave gracefully in case of interruption
time_lapsed = (datetime.now() - start_time)
print('Time elapsed (hh:mm:ss.ms) {}'.format(time_lapsed))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.