The Python Oracle

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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Thinking It Over

--

Chapters
00:00 How Do I Get My Program To Sleep For 50 Milliseconds?
00:12 Accepted Answer Score 1164
00:22 Answer 2 Score 123
00:34 Answer 3 Score 97
00:45 Answer 4 Score 19
01:45 Answer 5 Score 4
01:56 Thank you

--

Full question
https://stackoverflow.com/questions/3774...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #timer #sleep

#avk47



ACCEPTED ANSWER

Score 1164


Use time.sleep()

from time import sleep
sleep(0.05)



ANSWER 2

Score 123


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




ANSWER 3

Score 97


Use time.sleep():

import time
time.sleep(50 / 1000)

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




ANSWER 4

Score 4


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