How to make a Python program wait? - GeeksforGeeks

02 Dec, 2020
Prerequisites:  
time module
keyboard module
os module
Some requirements require a Python program to wait before it goes on. We might need another function to complete or a file to load to give the user a better experience. Discussed below are some ways by which this can be achieved.
1(A) General sleep function
Python has a module named time. This module gives several useful functions to control time-related tasks. sleep() is one such function that suspends the execution of the calling thread for a given number of seconds and returns void. The argument may be a floating-point number to indicate more precise sleep time. This is the most common method used because of its ease of use and the fact that it is platform independent. The implementation is given below:
Example:
Python3
# First import time module.import time  # immediately prints the following.print("GFG printed immediately.")time.sleep(5.5)  # delays the execution# for 5.5 secs.print("GFG printed after 5.5 secs.")
Output:
1(B) Sleep in multithreaded programming
For multithreaded python programs, the sleep() function suspends the current thread for a given number of seconds rather than the whole process. But for single-threaded programs sleep() function suspends the thread and the whole process. The implementation is given below:
Example:
Python3
# import threading and time module.import threadingimport time  def print_GFG():    for i in range(5):        # suspend the current thread.        time.sleep(1)        print("GFG")  def print_Geeksforgeeks():    for i in range(5):        # suspend the current thread.        time.sleep(1.5)        print("Geeksforgeeks")  # two threads are available in this program.t1 = threading.Thread(target=print_GFG)t2 = threading.Thread(target=print_Geeksforgeeks)t1.start()t2.start()
Output:
We all know that the input() function helps to take data from users. But with the help of this function, we can also pause a python script until a certain key is pressed, like in the following code:
Example:
Python3
print("GFG immediately")i = input("Press Enter to continue: ")  # pauses the script here# until the user press any key.print("GFG after the input.")
Output:
Using this module, we can resume the program by pressing the key that is specified in the python script (In this program the key is the ‘space‘ key). Keyboard module doesn’t come in-built with python, thus needs to be installed explicitly using the following command:
pip install keyboard
The implementation is given below:
Example:
Python3
# import keyboard module.import keyboard  # pause() function definition.def pause():    while True:        if keyboard.read_key() == 'space':            # If you put 'space' key            # the program will resume.            break    print("GeeksforGeeks printed before pause function")pause()print("GeeksforGeeks printed after pause function")
Output:
This module contains a function called interact(). Some non-programmers may like this simple method. This creates an interpreter that acts almost exactly like a real interpreter. This creates a new instance of Interactive Console and sets readfunc to be used as the InteractiveConsole.raw_input() method if provided.
Example:
For the program given below press (Ctrl+D) to resume.
Python3
# import codeimport code  print("GeeksforGeeks printed immediately.")  # implementation of code.interact().code.interact(banner='Paused. Press ^D (Ctrl+D) to continue.', local=globals())print("GeeksforGeeks.")
Output:
Os module contains a method called system(“pause”). Using this method we can make a python program wait until some key is pressed. But this method is platform dependent i.e. only works on windows. So, it is not widely used.
Example:
Python3
import os  print("GeeksforGeeks printed immediately.")os.system("pause")print("GeeksforGeeks.")
Output:
python-utility
Technical Scripter 2020
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python OOPs Concepts
How to Install PIP on Windows ?
Bar Plot in Matplotlib
Defaultdict in Python
Python Classes and Objects
Deque in Python
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python - Ways to remove duplicates from list
Class method vs Static method in Python