如何让Python程序等待?
先决条件:
- 时间模块
- 键盘模块
- 操作系统模块
某些要求要求Python程序在运行之前等待。我们可能需要另一个函数来完成或加载一个文件,以便为用户提供更好的体验。下面讨论了一些可以实现这一点的方法。
不同的方法和途径
Python时间模块
1(A) 一般睡眠函数
Python有一个名为time 的模块。 该模块提供了几个有用的函数来控制与时间相关的任务。 sleep() 就是这样一个函数,它将调用线程的执行挂起给定的秒数并返回 void。参数可能是一个浮点数,以指示更精确的睡眠时间。这是最常用的方法,因为它易于使用且与平台无关。实现如下:
例子:
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.")
Python3
# import threading and time module.
import threading
import 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()
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.")
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")
Python3
# import code
import code
print("GeeksforGeeks printed immediately.")
# implementation of code.interact().
code.interact(banner='Paused. Press ^D (Ctrl+D) to continue.', local=globals())
print("GeeksforGeeks.")
Python3
import os
print("GeeksforGeeks printed immediately.")
os.system("pause")
print("GeeksforGeeks.")
输出:
1(B) 在多线程编程中休眠
对于多线程Python程序, sleep() 函数将当前线程挂起给定的秒数,而不是整个进程。但是对于单线程程序sleep()函数会挂起线程和整个过程。实现如下:
例子:
蟒蛇3
# import threading and time module.
import threading
import 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()
输出:
2. 使用简单的 input()
我们都知道input()函数有助于从用户那里获取数据。但是在这个函数的帮助下,我们还可以暂停Python脚本,直到按下某个键,就像下面的代码:
例子:
蟒蛇3
print("GFG immediately")
i = input("Press Enter to continue: ")
# pauses the script here
# until the user press any key.
print("GFG after the input.")
输出:
3. 使用键盘模块
使用这个模块,我们可以通过按下Python脚本中指定的键来恢复程序(在这个程序中,键是“空格”键)。键盘模块不是内置于Python中的,因此需要使用以下命令显式安装:
pip install keyboard
实现如下:
例子:
蟒蛇3
# 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")
输出:
4. 使用代码模块
该模块包含一个名为interact() 的函数。一些非程序员可能喜欢这种简单的方法。这将创建一个解释器,其行为几乎与真正的解释器完全一样。这将创建一个新的 Interactive Console 实例,并将 readfunc 设置为用作 InteractiveConsole.raw_input() 方法(如果提供)。
例子:
对于下面给出的程序,请按 (Ctrl+D) 继续。
蟒蛇3
# import code
import code
print("GeeksforGeeks printed immediately.")
# implementation of code.interact().
code.interact(banner='Paused. Press ^D (Ctrl+D) to continue.', local=globals())
print("GeeksforGeeks.")
输出:
5. 使用 os 模块
Os 模块包含一个名为 system(“pause”) 的方法。使用这种方法,我们可以让Python程序等待,直到按下某个键。但是这种方法依赖于平台,即仅适用于 Windows。因此,它没有被广泛使用。
例子:
蟒蛇3
import os
print("GeeksforGeeks printed immediately.")
os.system("pause")
print("GeeksforGeeks.")
输出: