How to Sleep, Pause, Wait, or Stop your Python Code

Sat, Mar 14, 2009

Tech Tips

Here is the function to pause or stop your code in Python for a variable amount of time:

PYTHON:
  1. import time
  2. time.sleep(5) #will sleep for 5 seconds
  3.  
  4. time.sleep(.5) #will sleep for half a second
  5.  
  6. time.sleep(.1) #will sleep for a tenth of a second

This is useful if you are making numerous calls to web services and you want to slow things down so you get more consistent results and to not get your account or ip blocked.

It is similar to pause, wait and sleep in other languages.

Here is the 'time' module reference

An interesting note in the manual: the amount of time the code is actually stopped might be slightly longer due to other activity on your computer going on at the same time.

Bookmark and Share

3 Responses to “How to Sleep, Pause, Wait, or Stop your Python Code”

  1. loubatt Says:

    Nice,… very awesome… :D

  2. LouisHv2 Says:

    Its vunderbar i was able to make a dancing chocola kabelwurst with this code, lovely