Guest User

Custom (and more accurate) alternative for time.sleep in Python

a guest
Dec 9th, 2022
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. import time
  2.  
  3.  
  4. def sleep(duration, get_now=time.perf_counter):
  5.     """
  6.    Custom sleep function that works more accurate then time.sleep does.
  7.    Taken from: https://stackoverflow.com/a/60185893/3684575
  8.    :param duration: Duration to sleep (in seconds).
  9.    :param get_now: Function to retrieve current time (time.perf_counter by default)
  10.    :return:
  11.    """
  12.     now = get_now()
  13.     end = now + duration
  14.     while now < end:
  15.         now = get_now()
  16.  
Advertisement
Add Comment
Please, Sign In to add comment