Last active
June 7, 2023 16:23
-
-
Save brunomsantiago/9fc62c761c5a1e0daa93b0d21abe0496 to your computer and use it in GitHub Desktop.
Python function to block execution until desired timestamp, with countdown
This file contains 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
from time import sleep | |
from datetime import datetime, timedelta | |
def wait(until, | |
update_time=1, | |
waiting_message='Waiting...', | |
done_message='Done'): | |
print(waiting_message) | |
while ( | |
(countdown := until - datetime.now().replace(microsecond=0)) | |
.total_seconds() > 0 | |
): | |
print(countdown, end='\r') | |
sleep(update_time) | |
print(f'{countdown} {done_message}') | |
_now = datetime.now().replace(microsecond=0) | |
_until = _now + timedelta(seconds=20) | |
wait(_until) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment