亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

Python time.sleep():为您的代码添加延迟(示例)

什么是 Python 睡眠?

Python 睡眠() 是一个函数,用于将代码的执行延迟给定为 sleep() 的输入的秒数。 sleep() 命令是时间模块的一部分。您可以使用 sleep() 函数暂时停止代码的执行。例如,您正在等待进程完成或文件上传。

在本教程中,您将学习:

time.sleep() 语法

import time
time.sleep(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

使用(Python 3.4 或更高版本)提供的 asyncio.sleep 函数

您可以在 python 3.4 及更高版本中使用 asyncio.sleep。要使用 asyncio sleep 方法,需要在函数中添加 async 和 await,如下例所示:

示例:

该脚本有一个函数调用 display() 打印一条消息“欢迎来到 Guru99 教程”。函数 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 方法来自线程模块。 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 是线程可用的另一种方法,它有助于获得与 Python 时间睡眠相同的功能。 Timer的工作原理如下例所示:

示例:

Timer 将输入作为 Python 中的延迟时间(以秒为单位),以及需要启动的任务。要使计时器正常工作,您需要调用 start() 方法。在代码中,Timer 给定 5 秒,并且在 5 秒完成后必须调用函数 display。当 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

  1. Python 匿名/Lambda 函数
  2. Python 生成器
  3. Python 闭包
  4. Python 装饰器
  5. Python获取当前时间
  6. Python时间模块
  7. Python 睡眠()
  8. 带有示例的 Python Lambda 函数
  9. 带有示例的 Python round() 函数
  10. 带有示例的 Python map() 函数
  11. Python Timeit() 与示例
  12. Python 教程中的收益:生成器和收益与返回示例