The Python Oracle

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

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Secret Catacombs

--

Chapters
00:00 Question
00:19 Accepted answer (Score 1103)
00:31 Answer 2 (Score 114)
00:46 Answer 3 (Score 92)
01:02 Answer 4 (Score 15)
02:16 Thank you

--

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

Accepted answer links:
[time.sleep()]: https://docs.python.org/library/time.htm...

Answer 3 links:
https://docs.python.org/library/time.htm...

Answer 4 links:
[reference]: http://docs.python.org/library/time.html...
[Unix time]: https://en.wikipedia.org/wiki/Unix_time

--

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