You can use
time.sleep()method to sleep for milliseconds in Python.
If you want to delay python program by 500 ms, then you need to pass 0.5 as parameter to sleep method.Using codecs's decode() function to convert Hex to bytes in python
1234 import timetime.sleep(0.5)
Sometimes, the execution of a program needs to be suspended or paused for a given time. This is done so that the user can understand the whole program and plan the future steps of the program accordingly. In python, there is a provision by which execution of the current thread can be stopped for some time.
This tutorial will demonstrate some methods by which a program can sleep or stop its execution for some milliseconds in Python.
Table of Contents
time.sleep() function to make the program sleep for milliseconds
In this method, we use Python’s
time() module which helps in dealing with time in python and represents in the form of an object, a number, or even a string. This module also helps in measuring the execution speed and efficiency of the code.
time module, there is a function called
sleep() which is used to suspend the execution of a thread that is currently running for a given number of milliseconds.
Now, as we know that there are 1000 milliseconds in one second, so we need to convert if the input time is in milliseconds. For converting milliseconds to seconds, we multiply the given number of milliseconds by 0.001.
time_milliseconds = 10000
time_sec = 0.001 * time_milliseconds
time_start = time.time()
time_stop = time.time()
time_difference = time_stop - time_start
Note that we have converted the input time and we have stored the converted time in the variable called
time_sec variable. After that, we have used only the
time_sec variable for suspending the execution of the current code using the
threading.Timer() function to make the program sleep for milliseconds
In this method, we use the approach of
threads in a Python program. A thread is a specific task or a code block in a Python program. A thread can be referred to as a subpart of a large Python program. To deal with individual threads more efficiently, we use the
threading module in Python.
threading module helps in creating, managing, and controlling threads in Python. There are different objects and functions in this module to perform these tasks.
threading module contains a class
Timer that represents an action that should run when specified time has elapsed.
You can create a timer object that will run function after elapsed time. You need to start timer thread by calling
start() method on timer object.
from threading import Timer
print("This will print after sometime")
t.start() # after 500 milliseconds, "This will print after sometime" will be printed
- Defined a function in which we have printed message.
- We created timer object by passing time for sleep in milliseconds(ms) and function name
timerobject to start the thread.
Note: We can also deal with multiple threads in the same code and set the execution time of each thread individually using the
threading module. This process is called multithreading.
That’s all about how to sleep for milliseconds(ms) in Python.