JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Sleeping for a number of milliseconds in Python

Author:JIYIK Last Updated:2025/05/06 Views:

In this tutorial, we will look at various ways to pause or suspend the execution of a program in Python for a given amount of time. Let's say we want to pause the execution of a program for a few seconds to let the user read instructions about the next step of the program. We need some way to tell the program to go in for a specific number of seconds or milliseconds. We will also discuss a way to make the program call a provided function after a specific time interval without pausing or suspending the program execution.

In Python, we can use the following methods to pause or suspend the execution of a program for a given amount of time.


time.sleep()Using the sleep method in Python

time.sleep(secs)The method pauses or suspends the execution of the calling thread for secsthe number of seconds provided by the parameter. Therefore, we need to call time.sleep()the method to put the program into sleep state for a specific time.

The following sample code demonstrates how to use time.sleep()the method to make the program sleep for a specified number of seconds.

import time

time.sleep(1.5)
print("1.5 seconds have passed")

To pause the program for a few milliseconds, we need to divide the input 1000, as shown in the following example code:

import time

time.sleep(400 / 1000)
print("400 milliseconds have passed")

threading.Timer()Using the sleep method in Python

threading.Timer(interval, function, args, kwargs)The method waits for intervaltime equal to seconds and then calls the function with arguments argsand keyword arguments .kwargs

The method is useful if we want the program to wait for a specific amount of time and then call the function threading.Timer(). The following sample code demonstrates how to use threading.Timer()the method to make the program wait for intervalseconds before performing some task.

from threading import Timer


def nextfunction():
    print("Next function is called!")


t = Timer(0.5, nextfunction)
t.start()

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Writing logs to a file in Python

Publish Date:2025/05/06 Views:132 Category:Python

This tutorial will show you how to write logs to files in Python. Use the module in Python logging to write logs to files Logging is used to debug a program and find out what went wrong. logging The log module is used to log data to a file

Comparing two dates in Python

Publish Date:2025/05/06 Views:97 Category:Python

This tutorial explains how to compare two dates in Python. There are multiple ways to determine which date is greater, so the tutorial also lists different sample codes to illustrate the different methods. Comparing two dates in Python usin

Reload or unimport modules in Python

Publish Date:2025/05/06 Views:58 Category:Python

Modules allow us to store definitions of different functions and classes in Python files, which can then be used in other files. pandas , NumPy , scipy , Matplotlib are the most widely used modules in Python. We can also create our own modu

Pausing program execution in Python

Publish Date:2025/05/06 Views:156 Category:Python

This tutorial will demonstrate various ways to pause a program in Python. Pausing the execution of a program or application is used in different scenarios, such as when a program requires user input. We may also need to pause the program fo

Importing modules from a subdirectory in Python

Publish Date:2025/05/06 Views:190 Category:Python

This tutorial will explain various ways to import modules from subdirectories in Python. Suppose we have a file in a subdirectory of our project directory and we want to import this file and use its methods in our code. We can import files

If not statement in Python

Publish Date:2025/05/06 Views:123 Category:Python

The statement in Python if checks a specific condition and executes a block of code if the condition is true. if not The does the opposite of if the statement. It tests if a condition is false and then executes some statements. Using if not

Enumerating a dictionary in Python

Publish Date:2025/05/05 Views:98 Category:Python

The function in Python enumerate() returns an object of enumeration type and adds a counter variable to iterate over a list or other type of collection. It makes looping over such objects easier. When we pass an enumeration object to list()

Changing dictionary values in Python

Publish Date:2025/05/05 Views:109 Category:Python

This tutorial will discuss various ways to change the value of a particular key in Python dictionary. We can do this by using the following methods. dict.update() method for cycle. Dictionary Unpacking dict.update() How to change dictionary

Finding the maximum value in a Python dictionary

Publish Date:2025/05/05 Views:61 Category:Python

This tutorial explains how to get a key with the maximum value in Python. Since the method has changed from the previous Python versions, it also lists some sample codes to clarify the concepts. Use operator.itemgetter() the method to get t

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial