Python time.sleep(): 为代码添加延迟(示例)
什么是 Python Sleep?
Python sleep() 是一个函数,用于将代码的执行延迟输入到 sleep() 的秒数。sleep() 命令是 time 模块的一部分。您可以使用 sleep() 函数暂时停止代码的执行。例如,您正在等待一个进程完成或文件上传。
time.sleep() 语法
import time time.sleep(seconds)
参数
seconds:您希望代码执行暂停的秒数。
示例:在 Python 中使用 sleep() 函数
请按照以下步骤在您的 Python 脚本中添加 sleep()。
步骤 1
import time
步骤 2:添加 time.sleep()
输入到 sleep() 中的数字 5 是您希望代码执行暂停的秒数,当它被执行时。
time.sleep(5)
以下是一个工作代码,其中包含 print() 中的消息,以显示执行时终端上消息显示的延迟。
import time print("Welcome to guru99 Python Tutorials") time.sleep(5) print("This message will be printed after a wait of 5 seconds")
输出
Welcome to guru99 Python Tutorials This message will be printed after a wait of 5 seconds
如何使用 sleep() 延迟函数的执行?
下面所示的示例有一个名为 display() 的函数。display() 函数打印一条消息“Welcome to Guru99 Tutorials”。当调用该函数时,它将在终端中执行并显示消息。
为了延迟函数的执行,让我们在调用函数之前在 Python 中添加 time.sleep。在执行期间,Python time.sleep 将在此暂停给定的秒数,之后将调用 display() 函数。
示例
import time print('Code Execution Started') def display(): print('Welcome to Guru99 Tutorials') time.sleep(5) display() print('Function Execution Delayed')
输出
Code Execution Started Welcome to Guru99 Tutorials Function Execution Delayed
在 Python 脚本中添加延迟的各种方法是什么?
使用 sleep() 函数
我们之前已经看到了一些关于如何使用 time.sleep() 的示例。让我们在这里使用 time.sleep() 尝试一个不同的示例。
示例
该代码有一个 for 循环,它将获取字符串变量并以 1 秒的延迟打印每个字符。
import time my_message = "Guru99" for i in my_message: print(i) time.sleep(1)
输出
G u r u 9 9
使用 asyncio.sleep 函数 (Python 3.4 或更高版本)
您可以使用 Python 3.4 及更高版本的 asyncio.sleep。要使用 asyncio sleep 方法,您需要将 async 和 await 添加到函数中,如下面的示例所示。
示例
该脚本有一个函数调用 display(),它打印消息“Welcome to Guru99 tutorials”。函数中有两个关键字 async 和 await。async 关键字添加到函数定义的开头,await 添加到 asyncio.sleep() 之前。async / await 关键字都是用来处理异步任务的。
当调用 display() 函数时,它遇到 await asyncio.sleep(5),代码将在此暂停或挂起 5 秒,完成后将打印消息。
import asyncio print('Code Execution Started') async def display(): await asyncio.sleep(5) print('Welcome to Guru99 Tutorials') asyncio.run(display())
输出
Code Execution Started Welcome to Guru99 Tutorials
使用 Event().wait
Event().wait 方法来自 threading 模块。Event.wait() 方法将暂停任何进程的执行,持续作为参数的秒数。Event 的工作原理如下所示。
示例
代码使用 Event().wait(5)。数字 5 是代码延迟以调用 display() 函数的行之前要暂停的秒数。完成 5 秒后,将调用 display() 函数,并在终端中打印消息。
from threading import Event print('Code Execution Started') def display(): print('Welcome to Guru99 Tutorials') Event().wait(5) display()
输出
Code Execution Started Welcome to Guru99 Tutorials
使用 Timer
Timer 是 Threading 提供的另一种方法,它有助于获得与 Python time sleep 相同的功能。Timer 的工作原理如下所示。
示例
Timer 输入为 Python 中的延迟时间(秒)以及需要启动的任务。要使计时器工作,您需要调用 start() 方法。在代码中,Timer 被设置为 5 秒,并且 display 函数将在 5 秒后被调用。当调用 Timer.start() 方法时,计时器将开始工作。
from threading import Timer print('Code Execution Started') def display(): print('Welcome to Guru99 Tutorials') t = Timer(5, display) t.start()
输出
Code Execution Started Welcome to Guru99 Tutorials
摘要
- Python sleep() 函数将暂停 Python 代码或将程序的执行延迟到 sleep() 中输入的秒数。sleep() 函数是 Python time 模块的一部分。
- 当您想暂时停止代码执行时,可以使用 Python sleep 函数。例如,在您等待另一个进程完成或文件上传等情况下。
- 除了 sleep 之外,还有许多方法可以向代码添加 Python 延迟函数,例如使用 asyncio.sleep、Event().wait 和 Timer。
- 与 sleep() 方法类似,Python 3.4 及更高版本提供了 asyncio.sleep() 方法。要使用 asyncio sleep 方法,您需要将 async 和 await 添加到函数中。
- Event().wait 方法来自 threading 模块。Event.wait() 方法将暂停任何进程的执行,持续作为参数的秒数。
- Timer 是 Threading 提供的另一种方法,它有助于获得与 sleep 相同的功能。