The sleep() methodology in Python takes time to delay the execution of this system within the type of seconds. Nonetheless, it will possibly simply be made to work with milliseconds. To cease the execution of this system for some particular milli-seconds, then the consumer can move the time within the sleep() perform after dividing it by 1000 or multiplying it by 0.001.
This put up will cowl the strategies to change this system default execution move and delay it by a set time in milliseconds.
Methodology 1: Time Sleep in Milliseconds
The “time” library gives a perform “sleep()” that takes the delay interval in seconds, and one of the best half about this perform is that it will possibly take float values. This merely implies that it permits the customers to move the time in milliseconds as nicely. However somewhat than calculating the worth in floating level digits, it’s rather more handy to easily take time as milliseconds after which both divide it by 1000 or multiply it by 0.001.
Syntax of sleep() Operate With Milliseconds:
Let’s take an instance to showcase this methodology. For this, begin by importing the “time” library in your program utilizing the next line:
After that, set the time interval in milliseconds:
print(“The Program is reaching the sleep”)
Move this time interval within the sleep() methodology utilizing the next line:
time.sleep(sleepIntervalMS/1000)
Ultimately, after the sleep, immediate the consumer by printing the next:
print(“That is the output after sleep”)
The entire code snippet for this instance is as follows:
sleepIntervalMS = 1500
print(“The Program is reaching the sleep”)
time.sleep(sleepIntervalMS/1000)
print(“That is the output after sleep”)
When this code is executed, it produces the next output on the terminal:
As you may discover within the GIF above, there’s a slight delay of about 1.5 seconds or 1500 milliseconds within the execution of this system.
Methodology 2: Utilizing Timer() From Threading Bundle
That is an alternate resolution to the sleep() methodology of the time package deal. This timer() is used to execute a perform after a set period of time (often in seconds) has elapsed. Take a look at the syntax of the Timer() methodology given beneath.
Syntax of Timer() Methodology
timerVar = Timer(timeInterval, functionToCall)
On this syntax:
- timerVar is the variable that can be used to begin the timer utilizing the beginning() methodology.
- timeInterval will outline the period of time to be delayed earlier than calling the perform contained in the second argument.
- functionToCall is the perform that can be referred to as after the delay.
Be aware: Since this put up is worried with sleeping this system for some particular milliseconds, the consumer can move the time to Timer() methodology after dividing the time by 1000.
To exhibit an instance of working of this perform, begin by importing the Timer package deal from the threading library:
from threading import Timer
As soon as that’s finished, outline the interval period of the sleep in a variable:
Outline a perform that can be referred to as utilizing the Timer() methodology:
print(“This perform was executed after a delay”)
Name the Timer() methodology to set a timer on the perform created above utilizing the delay talked about above as nicely:
timeVar = Timer(delayMS/1000, basicPrompt)
Immediate the consumer that this system is about to enter sleep and name the beginning() methodology of the Timer variable:
timeVar.begin()
The entire code snippet for this instance is given beneath:
delayMS = 2500
def basicPrompt():
print(“This perform was executed after a delay”)
timeVar = Timer(delayMS/1000, basicPrompt)
print(“This system is reaching the sleep”)
timeVar.begin()
When this program is executed, it produces the next outcomes:
It may be seen within the output above that the perform was executed after two and a half seconds or after 2500 milliseconds. That additionally concludes this put up for sleep() in milliseconds.
Conclusion
The Python’s sleep() methodology from the time package deal can simply be made to delay the execution of this system for a set period of time which is handed within the type of milliseconds. To do that, the consumer has to both multiply the milliseconds by “.001” or divide it by a thousand whereas passing it within the argument of the sleep methodology. Alternatively, the consumer also can use the Timer() methodology, which has additionally been demonstrated on this article.