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

Question:

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

Asked By: TK.

||

Answers:

Use time.sleep():

import time
time.sleep(50 / 1000)

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

Answered By: Dan Olson

Use time.sleep()

from time import sleep
sleep(0.05)
Answered By: Will Boyce

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

Answered By: ya23

You can also use pyautogui as:

import pyautogui
pyautogui._autoPause(0.05, False)

If the first argument is not None, then it will pause for first argument’s seconds, in this example: 0.05 seconds

If the first argument is None, and the second argument is True, then it will sleep for the global pause setting which is set with:

pyautogui.PAUSE = int

If you are wondering about the reason, see the source code:

def _autoPause(pause, _pause):
    """If `pause` is not `None`, then sleep for `pause` seconds.
    If `_pause` is `True`, then sleep for `PAUSE` seconds (the global pause setting).

    This function is called at the end of all of PyAutoGUI's mouse and keyboard functions. Normally, `_pause`
    is set to `True` to add a short sleep so that the user can engage the failsafe. By default, this sleep
    is as long as `PAUSE` settings. However, this can be override by setting `pause`, in which case the sleep
    is as long as `pause` seconds.
    """
    if pause is not None:
        time.sleep(pause)
    elif _pause:
        assert isinstance(PAUSE, int) or isinstance(PAUSE, float)
        time.sleep(PAUSE)
Answered By: okie

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
Answered By: Abu Noman Md Sakib

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 boot using time.monotonic().

    This way is useful if you need a loop to be running. But this one is slightly more complex. time.monotonic is better than time.time as it does not account for leap seconds, but it counts the amount of settings from boot. (Credit: Mark Lakata)

    time_not_passed = True
    from time import monotonic as time # Importing time.monotonic but naming it 'time' for the sake of simplicity
    
    init_time = time() # Or time.monotonic() 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.monotonic() if whole module imported :O
            print("0.05 secs")
            time_not_passed = False
    
Answered By: Lucas Urban
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.