I need to make my python program wait for 200ms before polling for an input of some description. In C# for example, I could use Thread.Sleep() to achieve this. What is the simplest means of doing this in python?
4 Answers
Use Time module.
For example, to delay 1 second :
import time
time.sleep(1) # delay for 1 seconds
In your case, if you want to get 200 ms, use this instead:
time.sleep(0.2)
time.sleep also works with float.
Comments
Use the time library and use the command time.sleep() to make it wait. It's more efficient when choosing to extract it from the time library and then use just sleep() For Example:
import time
print('hi')
time.sleep(0.2)
print('hello')
Improved:
from time import sleep
print('Loading...')
sleep(2)
print('Done!')
Note: it is measured in seconds not ms.
5 Comments
Mark Amery
@cpburnz hey, you're being a little unfair suggesting that this answer isn't different from the others. The three answers you've linked to correctly use the
time.sleep() function, which, like, exists, while this one uses the time.wait() function, which doesn't exist. Being completely incorrect counts as being different, right? Right?Mark Amery
@cpburnz I fear your sarcasm detector has failed, sir. I'm not seriously suggesting that this answer gains value by being wrong; I'm entirely on your side that it's garbage in need of deletion (though I can't place a delete vote until it gets another downvote).
ohmu
@MarkAmery Sarcasm can be hard to detect in text especially when I'm sure I've seen people argue on Meta exactly that: any attempt to answer no matter how irrelevant or redundant should be kept because "it's an answer".
Matthew Miles
Oops, I don't know what I was thinking!