Python Sleep Milliseconds with Examples

To pause or sleep milliseconds in Python you can use the sleep() method with the fraction of a second as an argument that is equivalent to the milliseconds you would like to sleep. In Python, time.sleep() function is used to pause the execution of a program for a specified amount of time, to pause for a specific number of milliseconds, you can convert the number of milliseconds to seconds by dividing it by 1000 and then passing that value to the sleep() function.

Note that 1 second is equal to 1000 milliseconds.

In this article, I will explain the use of sleep for a specific number of milliseconds with examples.

1. Python Sleep Milliseconds

To sleep the execution for a specified number of milliseconds in Python, you need to divide the milliseconds by 1000 to get the faction of the second and pass it as an argument to the sleep() method. First, let’s see how this function works with seconds, and in the next example, I will show it with milliseconds.


# Import
import time

# sleep for 3 seconds
print('Sleep time: ', str(3), 'seconds')
time.sleep(3)
print('Woke up after: ', str(3), 'seconds')

# Output:
# Sleep time: 3 seconds
# Woke up after: 3 seconds

Here, the code first prints “Sleep time: 3 seconds” and then waits for 3 seconds before printing “Woke up after 3 seconds”.

To sleep the execution for 300 milliseconds (0.3 seconds) in Python, you can either divide this number by 1000 and pass the fraction of the second or use the expression directly to the time.sleep() function.

Since 1 second is equal to 1000 milliseconds, we are dividing the milliseconds by 1000 to get the fraction of the second.


# Import
import time

# Sleep 300 milliseconds
time.sleep(300/1000)
print('300 milliseconds passed')   

# Output:
# 300 milliseconds passed

The code waits for 0.3 seconds (300 milliseconds) before printing “300 milliseconds passed”. Note that the above sleep function takes delay time in seconds, so to pass 300 milliseconds we need to divide it by 1000.

Alternatively, you can also write it as.


# Sleep for 0.3 seconds, equivalent to 300 milliseconds
time.sleep(0.3)

2. Using the Timer() to Slepp Milliseconds

Uses the timer function from the Python threading module to schedule a function to be executed after sleeping for milliseconds. The Timer function takes two arguments: the first is the time delay in seconds, and the second is the function that should be executed.


from threading import Timer
# Using the Timer() function
def Sparkbyexamples():
  print("After 0.08 seconds, Sparkbyexamples will be printed")
result = Timer(0.08, Sparkbyexamples)
result.start() 

# Output
# After 0.08 seconds, Sparkbyexamples will be printed

# Use the Timer() function
from threading import Timer
def time_func():
    print("This text is printed after 800 milliseconds.")
result = Timer(0.8 , time_func)
result.start()  

# Output
# This text is printed after 800 milliseconds.

Alternatively, the time module in Python provides various functions and classes to work with time and dates, including the time() function which returns the current time in seconds. It also provides a sleep() function which can be used to pause the execution of a program for a specified amount of time, usually in seconds. If the input time is in milliseconds, as you mentioned, it must be converted to seconds before passing it to the sleep() function, as one second is equivalent to 1000 milliseconds. To convert milliseconds to seconds, we multiply the number of milliseconds by 0.002.


import time
time_in_millisec = 10000
time_sec = 0.002 * time_in_millisec
start_time = time.time()
time.sleep(time_sec)
stop_time = time.time()
result = stop_time - start_time
print(result)

# Output
# 20.00197720527649

Conclusion

In this article, I have explained how to sleep the python execution for certain milliseconds. since the function sleep() takes the seconds as an argument, you need to convert the milliseconds to seconds by dividing it by 1000. or use the fraction of a second as an argument to the function.

Happy Learning !!

Leave a Reply

You are currently viewing Python Sleep Milliseconds with Examples