Skip to content

Instantly share code, notes, and snippets.

@kendfss
Created March 26, 2021 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kendfss/2ef1fac235c783d85beb5db1035dbc7a to your computer and use it in GitHub Desktop.
Save kendfss/2ef1fac235c783d85beb5db1035dbc7a to your computer and use it in GitHub Desktop.
Simple little countdown timer to demonstrate asynchronous execution
"""
This script will count down from 5 and exit... unless you can press enter first ;)
"""
import keyboard, asyncio, time as t
async def get(key_checker):
await key_checker.wait()
return True
async def countdown(time):
while time > 0:
print(f'This program will exit in {int(time)} seconds. Press enter to start over.', end='\r')
await asyncio.sleep(1)
time -= 1
print(f'This program will exit in 0 seconds. Press enter to start over.', end='\r')
await asyncio.sleep(.75)
print('bye' + ' ' * len('This program will exit in 0 seconds. Press enter to start over.'))
return False
def notify(key_checker, event_loop):
def wrap(*args, **kwargs):
event_loop.call_soon_threadsafe(key_checker.set)
return wrap
async def main():
running = True
while running:
enter_pressed = asyncio.Event()
loop = asyncio.get_event_loop()
keyboard.on_press_key('enter', notify(enter_pressed, loop))
enter_pressed.clear()
clock = asyncio.create_task(countdown(5))
check = asyncio.create_task(get(enter_pressed))
done, pending = await asyncio.wait({clock, check}, return_when=asyncio.FIRST_COMPLETED)
print(done)
keyboard.unhook('enter')
[*map(lambda x: x.cancel(), pending)]
running = next(iter(done)).result()
if running:
print('Starting over' + ' ' * len('This program will exit in 0 seconds. Press enter to start over.'), end='\r')
await asyncio.sleep(1)
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment