richzilla
richzilla

Reputation: 41972

Make Python Program Wait

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?

Upvotes: 15

Views: 54787

Answers (4)

Matthew Miles
Matthew Miles

Reputation: 737

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.

Upvotes: 6

cnicutar
cnicutar

Reputation: 182619

If you simply want to sleep you can try:

import time

time.sleep(0.2)

Upvotes: 11

Zhong Xiaoqin
Zhong Xiaoqin

Reputation: 91

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)

Upvotes: 9

Thanakron Tandavas
Thanakron Tandavas

Reputation: 5683

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.

Upvotes: 29

Related Questions