15

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?

0

4 Answers 4

29

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.

Sign up to request clarification or add additional context in comments.

Comments

11

If you simply want to sleep you can try:

import time

time.sleep(0.2)

Comments

9

You can use the method sleep() in module time.

First you must import module time in your program. After that, you can call the sleep() function.

Add this to your code:

import time
time.sleep(0.2)

Comments

6

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

How is this any different from cnicutar's answer, Thanakron Tandavas's answer, or Zhong Xiaoqin's answer?
@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?
@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).
@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".
Oops, I don't know what I was thinking!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.