AppDividend
Latest Code Tutorials

Python Time sleep() Function Example | sleep() Method In Python

0

Python Time sleep() Function Example | sleep() Method In Python is today’s topic. The sleep() function suspends (waits) execution of the current thread for a given number of seconds. Python has a module named time which provides several useful functions to handle time-related tasks. One of the essential functions among them is sleep(). The sleep() function suspends execution of the current thread for a given number of seconds.

Python Time sleep()

The sleep() is defined in time() module of python 3 and 2 versions. In some cases, there is a requirement to hault the execution program so that several other executions can take place or only due to the utility required. Python sleep() can come handy in such a situation which provides the accurate and flexible way to hault the flow of code for any period.

The time.sleep() function is an equivalent to the Bash shell’s sleep command. Almost all programming languages have this feature and it is used in many use-cases.

#Syntax

time.sleep(secs)

It takes an argument called secs. It is the number of seconds, a Python program should pause execution. This argument should be either an int or float.

See the following code example.

# app.py

import time

# Wait for 3 seconds
time.sleep(3)
print('After 3 secs')

See the following output. It will print the statement after the 3 secs.

➜  pyt python3 app.py
After 3 secs
➜  pyt

#Python create a digital clock

See the following code to create the digital clock in Python.

import time
while True:
  localtime = time.localtime()
  result = time.strftime("%I:%M:%S %p", localtime)
  print(result)
  time.sleep(1)

In the above code, we have used the while loop.

See the following output.

➜  pyt python3 app.py
01:12:30 PM
01:12:31 PM
01:12:32 PM
01:12:33 PM
01:12:34 PM

#Different delay time of python sleep()

In some cases, you may need to delay for different seconds. See the following example.

# app.py

import time

for t in [ .3, .2, 1, 3]:
   print("Waiting for %s" % t , end='')
   print(" seconds")
   time.sleep(t)

See the following output.

➜  pyt python3 app.py
Waiting for 0.3 seconds
Waiting for 0.2 seconds
Waiting for 1 seconds
Waiting for 3 seconds
➜  pyt

#Python thread sleep

Python time sleep() function is a critical method for multithreading.

See the following example.

# app.py

import time
from threading import Thread


class Atapi(Thread):
    def run(self):
        for x in range(1, 12):
            print(x)
            time.sleep(1)


class Vatapi(Thread):
    def run(self):
        for x in range(100, 105):
            print(x)
            time.sleep(5)


print("Staring Atapi Thread")
Atapi().start()
print("Starting Vatapi Thread")
Vatapi().start()
print("Completed")

See the following output.

➜  pyt python3 app.py
Staring Atapi Thread
1
Starting Vatapi Thread
100
Completed
2
3
4
5
101
6
7
8
9
10
102
11
103
104
➜  pyt

From the output, it’s evident that only the threads are being stopped from execution and not the whole program by python time sleep function.

Finally, Python sleep() Method Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.