How To Add Time Delay in Python?

The caller thread's execution can be suspended for any amount of time using the sleep() function in the time module. One million times is the default quantity of times it will execute your code.

There are several ways to add a time delay in Python, including:

  1. Using the time.sleep() function: This function stops the current thread's execution for a predetermined number of seconds. For example, sleep(5) will delay the execution for 5 seconds.
  2. Using the datetime.timedelta class: This class can be used to represent duration and can be added to or subtracted from a datetime object. For example, time.sleep((datetime.datetime.now()+datetime.timedelta(seconds=5)-datetime.datetime.now()).total_seconds()) will delay the execution for 5 seconds.
  3. Using the sched module: This module lets you schedule functions to run after a predetermined amount of time has passed. To build a scheduler object and schedule a function to run at a specific time, use the scheduler() class.
  4. Using the signal module: The signal can be used in this module to schedule a function to run after a set amount of time has passed .alarm(time) function, where time represents the duration in seconds before the function is executed.
  5. Using the threading module: This module allows you to create new threads, you can use the Timer(time, function) class to create a timer object and run a function after a specific time delay.

Approach 1: Using time.sleep() function

Steps:

  • Importing the time module is the first essential step.
  • To introduce a temporal delay during execution, the sleep() method is used between the two statements between which the delay is needed. giving a number as the sleep() function parameter, either an integer or a float.
  • Next, run the corresponding program.
  • Once the code has run, keep track of the execution time delay.

Example code:

Output:

How To Add Time Delay in Python

Approach 2: Using threading.Event. wait function

In the approach of threading.Event.wait for function, thread suspends until the successful execution of the set() method is completed. It can take a parameter that can be used as a specific time, which means the execution will stop until the specified time has passed.

Threading.Event.wait() is the syntax to be followed to use this approach to add a time delay in any code.

Example:

Output:

How To Add Time Delay in Python

Approach 3: Using threading.Timer class

Timer objects represent tasks that need to start at a specified time. These things are set to run on a separate thread from the one that executes the action.

Example:

Output:

Thread Timer 
javaTpoint

Approach 4: Using the datetime module

Date and time are not separate data types in Python, but they can be used together by importing the datetime package. Install the Python Datetime Module outside is not required outside because it is already included in Python.

Classes for working with date and time are provided by the Python datetime module. Numerous capabilities to deal with dates, times, and time periods are provided by these classes.When working with date and datetime in Python, you're actually working with objects rather than strings or timestamps because the language recognizes date and datetime as things.

For example:

Output:

How To Add Time Delay in Python

Approach 5:Using Sched module in Python

The Sched module, which serves as the industry standard library, can be used to build bots and other automation and monitoring programs. A general event scheduler for running jobs at certain times is implemented by the sched module. It offers tools comparable to task schedulers in Windows or Linux, but the key benefit is that platform differences may be ignored according to Python's own sched module.

For example:

Output:

Starting...
...Finished

These are examples of time delays in python. It's important to note that the time.sleep() function will cause the entire program to pause for the specified amount of time, while the datetime and sched examples will only delay the execution of a specific section of code.






Latest Courses