The sleep() method in Python takes time to delay the execution of the program in the form of seconds. However, it can easily be made to work with milliseconds. To stop the execution of the program for some specific milli-seconds, then the user can pass the time in the sleep() function after dividing it by 1000 or multiplying it by 0.001.
This post will cover the methods to alter the program default execution flow and delay it by a set time in milliseconds.
Method 1: Time Sleep in Milliseconds
The “time” library provides a function “sleep()” that takes the delay interval in seconds, and the best part about this function is that it can take float values. This simply means that it allows the users to pass the time in milliseconds as well. But rather than calculating the value in floating point digits, it is much more convenient to simply take time as milliseconds and then either divide it by 1000 or multiply it by 0.001.
Syntax of sleep() Function With Milliseconds:
Let’s take an example to showcase this method. For this, start by importing the “time” library in your program using the following line:
After that, set the time interval in milliseconds:
print("The Program is reaching the sleep")
Pass this time interval in the sleep() method using the following line:
In the end, after the sleep, prompt the user by printing the following:
The complete code snippet for this example is as follows:
sleepIntervalMS = 1500
print("The Program is reaching the sleep")
time.sleep(sleepIntervalMS/1000)
print("This is the output after sleep")
When this code is executed, it produces the following output on the terminal:
As you can notice in the GIF above, there is a slight delay of about 1.5 seconds or 1500 milliseconds in the execution of the program.
Method 2: Using Timer() From Threading Package
This is an alternate solution to the sleep() method of the time package. This timer() is used to execute a function after a set amount of time (usually in seconds) has elapsed. Look at the syntax of the Timer() method given below.
Syntax of Timer() Method
In this syntax:
- timerVar is the variable that will be used to start the timer using the start() method.
- timeInterval will define the amount of time to be delayed before calling the function inside the second argument.
- functionToCall is the function that will be called after the delay.
Note: Since this post is concerned with sleeping the program for some specific milliseconds, the user can pass the time to Timer() method after dividing the time by 1000.
To demonstrate an example of working of this function, start by importing the Timer package from the threading library:
Once that is done, define the interval duration of the sleep in a variable:
Define a function that will be called using the Timer() method:
print("This function was executed after a delay")
Call the Timer() method to set a timer on the function created above using the delay mentioned above as well:
Prompt the user that the program is about to go into sleep and call the start() method of the Timer variable:
timeVar.start()
The complete code snippet for this example is given below:
delayMS = 2500
def basicPrompt():
print("This function was executed after a delay")
timeVar = Timer(delayMS/1000, basicPrompt)
print("The program is reaching the sleep")
timeVar.start()
When this program is executed, it produces the following results:
It can be seen in the output above that the function was executed after two and a half seconds or after 2500 milliseconds. That also concludes this post for sleep() in milliseconds.
Conclusion
The Python’s sleep() method from the time package can easily be made to delay the execution of the program for a set amount of time which is passed in the form of milliseconds. To do this, the user has to either multiply the milliseconds by “.001” or divide it by a thousand while passing it in the argument of the sleep method. Alternatively, the user can also use the Timer() method, which has also been demonstrated in this article.