Python's time module has a handy function called sleep(). Essentially, as the name implies, it pauses your Python program. time.sleep() is the equivalent to the Bash shell's sleep command. Almost all programming languages have this feature, and is used in many use-cases.

Python's time.sleep() Syntax

This is the syntax of the time.sleep() function:

time.sleep() Arguments

  • secs - The number of seconds the Python program should pause execution. This argument should be either an int or a float.

Using Python's time.sleep()

Here's a quick, simple example to the syntax:

More Advanced Syntax

Here's a more advanced example. It takes user input, and asks you how long you want to sleep(). It also proves how it works, by printing out the timestamp before, and after the time.sleep() call. Note that Python 2.x uses the raw_input() function to get user input, whereas Python 3.x uses the input() function.








The Accuracy of time.sleep()

The time.sleep() function uses the underlying operating system's sleep() function. Ultimately there are limitations of this function. For example on a standard Windows installation, the smallest interval you may sleep is 10 - 13 milliseconds. The Linux kernels tend to have a higher tick rate, where the intervals are generally closer to 1 millisecond. Note that in Linux, you can install the RT_PREEMPT patch set, which allows you to have a semi-realtime kernel. Using a real-time kernel will further increase the accuracy of the time.sleep() function. Generally however, unless you want to sleep for a very small period, you can generally ignore this information.

Here's a few links on the information above:

  • W. Patrick Jones

    I have a small script that, when I press a button, turns on an LED and a few other things, then timer.sleep(1200), then takes more action and turns off the LED. I’d like to add some code so that, if the user presses the button again, the timer ends and the next steps happen immediately. Is a For loop with a short timer and a test for button press the best approach?

    • http://jacksonc.com Jackson Cooper

      If it’s a GUI I’d go with an asynchronous/button press event callback solution. The time.sleep() call would be in a thread, and the button press event callback would do a check to see if the sleep thread is active, and if so end it.

      • W. Patrick Jones

        Not a GUI. Physical button. I’m brand new to Python and still learning a lot. Just using straight Python on Raspian. Thanks for the quick response.

        • http://jacksonc.com Jackson Cooper

          Ah I see. I haven’t used the Raspberry Pi before (looks very tempting though :)) but after a quick search, the concept is the same. It appears you can map the button press event to a callback (function/method): http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-2. So my_callback could store the state of the button (e.g. how many times it’s been pressed), and do whatever logic it needs to do when called.

    • http://jacksonc.com Jackson Cooper

      This may be of use if you’re using PySide or PyQt: http://pythoncentral.io/pyside-pyqt-tutorial-using-built-in-signals-and-slots/

  • Vaidotas

    In python 2.x print is not a fuction.

  • Dimitri Mies

    I don’t know why, but for me when I run it it just says ” time.sleep(1)
    AttributeError: ‘int’ object has no attribute ‘sleep’ “

    • Elieroz

      Maybe you’ve got a variable called “time”. In that case, you should rename it to something else, avoiding names of other module, functions, classes…, already in use.