Python time.sleep():为您的程序添加延迟 Code (例子)
什么是 Python 睡了吗?
Python 睡觉() 是一个函数,用于延迟执行代码,延迟时间为 sleep() 输入的秒数。sleep() 命令是时间模块的一部分。您可以使用 sleep() 函数暂时停止代码的执行。例如,您正在等待某个进程完成或文件上传。
time.sleep() 语法
import time time.sleep(seconds)
参数
秒:您希望暂停代码执行的秒数。
示例:在中使用 sleep() 函数 Python
按照以下步骤在你的 Python 脚本中添加 sleep()。
第三步:
import time
第三步: 添加 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() 延迟函数的执行?
The example shown below has a function defined called display(). The display() function prints a message "Welcome to Guru99 Tutorials". When the function is called, it will execute and display the message inside the terminal.
为了增加函数执行的延迟,我们在 Python 在调用该函数之前。在执行过程中, 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,如下例所示:
计费示例:
The script has a function call display() that prints a message "Welcome to Guru99 tutorials". There are two keywords used in the function async and await. The async keyword is added at the start of the function definition, and await is added just before the asyncio.sleep(). Both the keywords async / await are meant to handle the asynchronous task.
当调用函数 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 方法来自线程模块。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
使用定时器
计时器是线程提供的另一种方法,它有助于获得与 Python time sleep。Timer 的工作原理如下例所示:
计费示例:
定时器接受输入作为延迟时间 Python 以秒为单位,以及需要启动的任务。要使计时器工作,您需要调用start()方法。在代码中,Timer被赋予5秒,并且显示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 时间模块。
- 你可以利用 Python 当您想暂时停止执行代码时,可以使用 sleep 函数。例如,当您在等待另一个进程完成或文件上传等时。
- 有很多方法可以添加 Python 除了睡眠之外,还可以使用延迟函数来编码,它们使用 asyncio.sleep , Event().wait 和 Timer。
- 与 sleep() 方法类似,python 3.4 及更高版本中有一个 asyncio.sleep() 方法。要使用 asyncio sleep 方法,您需要将 async 和 await 添加到函数中
- Event().wait 方法来自线程模块。Event.wait() 方法将暂停任何进程的执行,暂停时间为参数指定的秒数。
- Timer 是线程中可用的另一种方法,它有助于获得与 sleep 相同的功能
