Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Sync sleep in loop

import time

def sleep(cnt, sec):
    print(f"Start {cnt}")
    time.sleep(sec)
    print(f"End {cnt}")

def main():
    for i in range(4):
        sleep(i, 1)


start = time.monotonic()
main()
end = time.monotonic()
print(f"Elapsed {end-start}")
Start 0
End 0
Start 1
End 1
Start 2
End 2
Start 3
End 3
Elapsed 4.004362344741821