Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Telnet - Python Network programming
Next article icon

How to make a Python program wait?

Last Updated : 17 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The goal is to make a Python program pause or wait for a specified amount of time during its execution. For example, you might want to print a message, but only after a 3-second delay. This delay could be useful in scenarios where you need to allow time for other processes or events to occur before continuing with the program. Let’s explore different methods to achieve this efficiently.

Using time.sleep()

time.sleep() is used to pause the entire program for a given number of seconds. During this time, the program does nothing and waits, blocking all other operations. It’s a simple and blocking delay.

Python
import time  print("Wait for 3 seconds...") time.sleep(3) print("Done waiting!") 

Output

Output

Using time.sleep()

Explanation: This code pauses the program for 3 seconds. During this time, the program does nothing and waits. After 3 seconds, it continues and prints “Done waiting!”. It’s a blocking delay, meaning no other tasks can run during this time.

Using asyncio.run()

This method is used in asynchronous programming. asyncio.sleep() pauses only the current async task without blocking the rest of the program, allowing other tasks to run simultaneously. It’s a non-blocking delay.

Python
import asyncio  print("Wait for 3 seconds...") asyncio.run(asyncio.sleep(3)) print("Done waiting!") 

Output

Output

Using asyncio.run()

Explanation: asyncio.run() function runs the asynchronous task and once 3 seconds have passed, it continues to the next line and prints “Done waiting!”.

Using threading.Event().wait()

This method pauses only the thread that calls it, without affecting other threads. It’s useful when working with multiple threads and you want to delay or synchronize one of them without blocking everything.

Python
import threading  print("Wait for 3 seconds...") threading.Event().wait(3) print("Done waiting!") 

Output

Output

Using threading.Event().wait()

Explanation: This code pauses the current thread for 3 seconds. Other threads can continue running during this time. After the delay, it prints “Done waiting!”. This is a thread-specific blocking delay.

Using sched.scheduler().enter()

This schedules a function to be executed after a delay. It doesn’t pause the program but registers a task to run later, allowing the rest of the code to continue running in the meantime.

Python
import time import sched  s = sched.scheduler(time.time, time.sleep)  print("Scheduling task...") s.enter(3, 1, lambda: print("Executed after 3 seconds!")) s.run() 


Output

Output

Using sched.scheduler().enter()

Explanation: sched.scheduler(time.time, time.sleep) creates a scheduler using the current time and time.sleep for managing delays. s.enter(3, 1, lambda: print(“Executed after 3 seconds!”)) schedules a task to run after 3 seconds with priority 1. Finally, s.run() starts the scheduler and executes the task after the delay.



Next Article
Telnet - Python Network programming
author
tenacious39
Improve
Article Tags :
  • Python
  • Python Programs
  • Technical Scripter
  • python-utility
  • Technical Scripter 2020
Practice Tags :
  • python

Similar Reads

  • How to Parallelize a While loop in Python?
    Parallelizing a while loop in Python involves distributing the iterations of a loop across multiple processing units such as the CPU cores or computing nodes to execute them concurrently. This can significantly reduce the overall execution time of the loop, especially for tasks that are CPU-bound or
    2 min read
  • How to Keep a Python Script Output Window Open?
    We have the task of how to keep a Python script output window open in Python. This article will show some generally used methods of how to keep a Python script output window open in Python. Keeping a Python script output window open after execution is a common challenge, especially when running scri
    2 min read
  • Telnet - Python Network programming
    Telnet is a networking protocol that follows a client-server model. It uses TCP as its underlying communication protocol. It is typically used to start and a remote command-line session, typically on a server. Some facts about telnet:Uses Transmission Control Protocol for data transmission.Bi-direct
    5 min read
  • Measure time taken by program to execute in Python
    Measuring the execution time of a Python program is useful for performance analysis, benchmarking, and optimization. Python provides several built-in modules to achieve this with ease. In this article, we'll explore different ways to measure how long a Python program takes to run. Using the time Mod
    3 min read
  • How to Emulate a Do-while loop in Python?
    We have given a list of strings and we need to emulate the list of strings using the Do-while loop and print the result. In this article, we will take a list of strings and then emulate it using a Do-while loop. Do while loop is a type of control looping statement that can run any statement until th
    3 min read
  • Running Python program in the background
    Let us see how to run a Python program or project in the background i.e. the program will start running from the moment device is on and stop at shut down or when you close it. Just run one time to assure the program is error-bug free One way is to use pythonw, pythonw is the concatenation of python
    3 min read
  • How to Kill a While Loop with a Keystroke in Python?
    While loops are used to repeatedly execute a block of code until a condition is met. But what if you want the user to stop the loop manually, say by pressing a key? Then you can do so by pressing a key. This article covers three simple ways to break a while loop using a keystroke: Using KeyboardInte
    2 min read
  • How to Close MP3 Files Using Python?
    Manipulating audio files, such as MP3s, is a common task in Python programming, especially in applications involving audio processing, music players, or audio analysis. This article will explore concepts related to this topic and guide how to effectively close MP3 files in Python. Closing MP3 files
    2 min read
  • Personalized Task Manager in Python
    In this article, we are going to create a task management software in Python. This software is going to be very useful for those who don't want to burden themselves with which task they have done and which they are left with. Being a coder we have to keep in mind which competition is going on, which
    10 min read
  • How To Create a Countdown Timer Using Python?
    In this article, we will see how to create a countdown timer using Python. The code will take input from the user regarding the length of the countdown in seconds. After that, a countdown will begin on the screen of the format 'minutes: seconds'. We will use the time module here. Step-by-Step Approa
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences