Skip to content

Instantly share code, notes, and snippets.

@BBarbosa
Created August 1, 2019 15:01
Show Gist options
  • Save BBarbosa/3ee868e281b24c51b5fa7eb9c1f558bc to your computer and use it in GitHub Desktop.
Save BBarbosa/3ee868e281b24c51b5fa7eb9c1f558bc to your computer and use it in GitHub Desktop.
Similiar implementation of windows timeout command
"""
Python function to simulate Windows timeout command
NOTE: It may not work on Linux due to mvscrt library
"""
from msvcrt import getch, kbhit
from time import sleep
def timeout(seconds=0):
"""Similar Windows command line timeout implementation
Keyword Arguments:
seconds {int} -- Timeout lenght (default: {0})
"""
if not isinstance(seconds, int) or seconds < 0:
raise ValueError("The timeout lenght should be an integer >= 0")
message = "\rWaiting for %d seconds, press any key to continue..."
n_prints = seconds + 1
for i in range(n_prints):
if kbhit():
getch()
break
print(message % (seconds - i), end="")
sleep(1)
if __name__ == "__main__":
timeout(12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment