How do I get my Python program to sleep for 50 milliseconds?

652,396

Solution 1

Use time.sleep()

from time import sleep
sleep(0.05)

Solution 2

Note that if you rely on sleep taking exactly 50 ms, you won't get that. It will just be about it.

Solution 3

Use time.sleep():

import time
time.sleep(50 / 1000)

See the Python documentation: https://docs.python.org/library/time.html#time.sleep

Solution 4

There is a module called 'time' which can help you. I know two ways:

  1. sleep

    Sleep (reference) asks the program to wait, and then to do the rest of the code.

    There are two ways to use sleep:

    import time # Import whole time module
    print("0.00 seconds")
    time.sleep(0.05) # 50 milliseconds... make sure you put time. if you import time!
    print("0.05 seconds")
    

    The second way doesn't import the whole module, but it just sleep.

    from time import sleep # Just the sleep function from module time
    print("0.00 sec")
    sleep(0.05) # Don't put time. this time, as it will be confused. You did
                # not import the whole module
    print("0.05 sec")
    
  2. Using time since Unix time.

    This way is useful if you need a loop to be running. But this one is slightly more complex.

    time_not_passed = True
    from time import time # You can import the whole module like last time. Just don't forget the time. before to signal it.
    
    init_time = time() # Or time.time() if whole module imported
    print("0.00 secs")
    while True: # Init loop
        if init_time + 0.05 <= time() and time_not_passed: # Time not passed variable is important as we want this to run once. !!! time.time() if whole module imported :O
            print("0.05 secs")
            time_not_passed = False
    

Solution 5

You can also do it by using the Timer() function.

Code:

from threading import Timer

def hello():
  print("Hello")

t = Timer(0.05, hello)
t.start()  # After 0.05 seconds, "Hello" will be printed
Share:
652,396
TK.
Author by

TK.

--

Updated on December 09, 2020

Comments

  • TK.
    TK. over 3 years

    How do I get my Python program to sleep for 50 milliseconds?

  • aman.gupta
    aman.gupta over 15 years
    It might be 10 or 15ms longer than that on some platforms, so be warned.
  • user391339
    user391339 about 6 years
    Is it a consistent delay on a given system?
  • Chris
    Chris over 5 years
    @CsabaToth as long as you have a Python implementation that is up to spec on your OS, the above is OS independent.
  • David
    David over 5 years
    @user391339 From experience it is not consistent. Thread/process priority, CPU load avg, available memory, and a plethora of other factors make all calls imprecise. The busier the system is, the higher the imprecision.
  • okie
    okie over 4 years
    somehow, it's better to use time.sleep rather then this, but if you want your program to be pure autopygui, then this can be a way .
  • Elias Strehle
    Elias Strehle over 4 years
    Might be interesting to know though that 'the function [time.sleep(secs)] sleeps at least secs' since Python 3.5 according to the documentation.
  • Peter Mortensen
    Peter Mortensen over 3 years
    But how does it actually work? E.g., will the actual time resolution often be 16.66 ms (1/60 second)? In this particular case the sleep time happens to be exactly 3 times the time resolution. However, what about rounding? What if 3 is actually 2.9999999 due to floating point operations and it is rounded down to 2 (actual sleep time = 0.0333333 s = 33.33 ms)?
  • Peter Mortensen
    Peter Mortensen over 3 years
    How does it actually work for sub-second sleep? Often timers have a time resolution of 16.66 ms.
  • Mark Lakata
    Mark Lakata about 2 years
    It is not recommended to use time.time() for measuring elapsed time. It is better to use time.monotonic() which is guaranteed to increase at a uniform rate. There are actual cases where time() can change by jumps, because of leap seconds and things. time.monotonic() has no absolute correlation to Linux epoch time, but it is usually started at 0 when the system boots up.
  • Mark Lakata
    Mark Lakata about 2 years
    It is extremely unlikely that the python code base will round sleep(0.05) into 2 system clock ticks. It will mostly likely request 3 clock ticks because that's the right answer. BUT the system might return after 3 or 4 or 5 or 100 clock ticks. There's no guarantee it will return after 3 clock ticks if it is busy doing something else like flush data to the disk. Definitely DON'T use this if timing is critical. You would have to write code at the driver level to leverage interrupts if you want super accurate sleep intervals.
  • Anton
    Anton about 2 years
    pyautogui uses time.sleep()