How to sleep for milliseconds(ms) in Python

Sleep for milliseconds in python

💡 Outline
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.

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.

Use 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.

In Python’s 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.

Example:

Output:

Sleep Time= 10.004930019378662

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 sleep() function.

Use 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.

The 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.

Example:

Output:

This will print after sometime

Explanation

  • Defined a function in which we have printed message.
  • We created timer object by passing time for sleep in milliseconds(ms) and function name somefunction as argument.
  • Called start() on timer object 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.

Related Posts

  • Print blank line in Python
    23 September

    Print blank line in Python

    Table of ContentsWhat is a blank line in Python?Ways to print blank line in PythonUsing an empty print() functionUsing the print() function with newline characterUsing the print() function with an empty stringUsing the print() function with the sep parameterWays to print multiple blank lines in PythonUsing the print() function with multiple newline charactersUsing the print() […]

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

  • Write array to CSV in Python
    30 August

    Python write array to CSV

    Table of ContentsUsing the pandas.to_csv() function to write array to CSV fileUsing the numpy.tofile() function to write array to CSV fileUsing the numpy.savetxt() function to write array to CSV fileUsing the file-handling methods to write array to CSV fileUsing the writerows() function to write array to CSV file A CSV file is a simple text […]

  • Multiply in Python
    30 August

    Multiply in Python

    Table of ContentsMultiply operator in PythonMultiplying a string with an integerMultiplying all the elements of a listUse the for loop to multiply all the elements of a listUse the functools.reduce() function to multiply all the elements of a listUse the numpy.prod() function to multiply all the elements of a listUse the math.prod() function to multiply […]

  • Auto clicker in Python
    30 August

    How to make auto clicker in Python

    Table of ContentsUsing the pynput module to create an auto clickerUsing the pyautogui module to create an auto clickerUsing the pywin32 (winauto32api) module to create an auto clicker Python has inbuilt modules and methods which allow it to detect and perform keyboard and mouse inputs. We can create an auto-mouse clicker in Python. An auto-clicker […]

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.