How to Sleep/Delay Program Execution in Milliseconds in Python

The time module in Python contains several useful methods for the execution of time-related tasks. One of the most common methods of this module is the sleep() method which is used to add a delay to the threads currently running the program in Python. The delay in the execution occurs for some time in seconds.

In this article, we will discuss the sleep() method with time in milliseconds in Python.

How to Sleep/Delay Program Execution in Milliseconds in Python?

The sleep() function in Python is invoked/executed using the time module. The fraction of time is specified for which the code is to be delayed. Since it by default takes the time in seconds, therefore, to print the time in milliseconds either the sleep time is multiplied by 0.001 or divided by 1000 since 1 sec=1000ms. In addition to this, Python provides several other methods that help us delay the code for a specific duration. These methods are 

  1. Sleep/Delay Program Execution Using time.sleep() 
  2. Sleep/Delay Program Execution Using thread.timer()
  3. Pause/Resume Program Execution Using Keyboard Module

Method 1: Sleep/Delay Program Execution Using time.sleep()

The “time.sleep()” function takes time as a parameter and sleeps for the specified time. By default, it takes the given time in seconds, however, the specified time can be customized in any other time unit, such as milliseconds.

Example 1: Halt Program Execution in Milliseconds

The time.sleep() function is implemented in the code below to halt the program execution for the specified milliseconds.

#Import the time module
import time
#the time to sleep for
milliseconds_time = 5000
time_sec = 0.001 * milliseconds_time
start = time.time()
print("The start time is :", start)
time.sleep(time_sec)
stop = time.time()
print("The stop time is:", stop)
sleeping_time = stop - start
print("The Sleeping time is: ",sleeping_time)

In the above code block:

  • The time module is imported.
  • The sleep time is declared as “milliseconds_time” with a delay of 5000 which is in seconds.
  • In the next step, the declared sleep time is multiplied by 0.001 to convert it into milliseconds.
  • The start and stop time are declared as time.time() which returns the starting time and stopping in seconds.
  • The sleeping time or execution delay time is calculated and printed using the print() function.

Output

The output below demonstrates the start time of code execution, the end time of the code execution, and the sleeping time.

Example 2: Take User Input to Halt Program Execution

In the following code example, we will take input from the user to delay the program execution accordingly:

#Import the time module
import time
#the time to sleep for
x=int(input("Enter the time in milliseconds: "))time_sec = 0.001 * x
start = time.time()
print("The start time is :", start)
time.sleep(time_sec)
stop = time.time()
print("The stop time is:", stop)
sleeping_time = stop - start
print("The Sleeping time is: ",sleeping_time)

In the above code,

  • The int() function is used along with the input() function to take an integer value from the user as input.
  • The user-entered value is multiplied with the “.001” to get/convert it into milliseconds.
  • The rest of the steps/procedure is the same as the above example.

Output

The below output depicts that “14000” is entered as the input and the results are printed for that input.

Method 2: Sleep/Delay Program Execution Using thread.timer()

The “timer()” method belongs to the threading module and is used to make a delay for the specified time. The syntax of the Python’s timer() function is depicted below:

Timer(delay_time, function)

The two arguments of this function are

  • delay_time: the time duration for which the delay will occur.
  • function: the function on which the time delay will be implemented/executed.

Example: Executing thread.timer() Function

The code below implements the timer() function of the threading module to delay the program execution for the specified time.

from threading import Timer
def sleep_function():
    print(t)
#sleep for the specified time
t = Timer(0.001 , sleep_function)
t.start()

In the above code block:

  • The “Timer” is imported from the “threading” module of Python.
  • In the next step, a user-defined function is declared as a “sleep_function()”.
  • The Timer is defined as “t” with the delay time as “0.001ms” and the function as “sleep_function”.
  • The start() method starts the thread activity.

Output

The timer function sleeps the code for the specified time and then resumes the program execution.

Method 3: Pause/Resume Program Execution Using Keyboard Module

In Python, we can import/utilize the keyboard module to create a delay in the program execution based on the keyboard keys. For instance, using the keyboard module we can specify a particular key (like space, tab, etc.) based on which the program execution can be paused or resumed. 

To use the keyboard module, first, it needs to be installed on your system. To do that, the below-stated pip command must be executed:

pip install keyboard

Once the keyboard module is successfully installed, you can import and utilize it according to the program requirements.

Example: Use Keyboard Module to Delay a Program Execution in Milliseconds

In the following code, the keyboard module will be utilized along with the time module to pause the program execution in milliseconds.

import keyboard
import time
def delay():
  while True:
    if keyboard.read_key() == 'tab':
    #program will resume when you press the "tab" key
    break

print("Current Time Before Delay", (time.time()*1000))
delay()
print("Current Time After Delay", (time.time()*1000))

In this code block:

  • First, the keyboard and time modules are imported.
  • A user-defined function is created in which a keyboard key is specified based on which the program will pause or resume. In our case, the “tab” key is used for the delay, you can use any key of your choice.
  • The “time.time()” function is multiplied by “1000” to get the current time in milliseconds.
  • The user-defined delay() function is invoked to delay the program execution until the tab is pressed from the keyboard.

Output

The time (in milliseconds) before and after the delay is printed using the print() function.

This is how you can delay program execution in milliseconds in Python.

Conclusion

The execution of a Python program can be delayed in milliseconds using two different functions that are the sleep() function of the timer module and the Timer() function of the threading module. The sleep time must be multiplied by 0.001 or divided by 1000 to sleep/delay the program execution in Milliseconds. In this write-up, we have implemented the Timer() and the sleep() methods to create a delay in milliseconds in Python.

Over 500 Million Views on Our Blogs and YouTube! We’re inviting 10 businesses to partner with us and grow together Click here to join us ×