专业编程基础技术教程

网站首页 > 基础教程 正文

一日一技:向Python中添加延迟函数time.sleep()

ccvgpt 2024-08-15 20:23:17 基础教程 7 ℃



向Python中添加延迟time.sleep()


什么是Python Sleep?

一日一技:向Python中添加延迟函数time.sleep()

Python sleep()函数会将代码的执行延迟提供给sleep()输入的秒数。 sleep()命令是时间模块的一部分。 当我们要暂时执行代码时,可以使用sleep()函数。 例如,正在等待过程完成或文件上传等。



在本教程中,我们将学习:

  • 什么是Python Sleep?
  • 语法
  • 示例:在Python中使用sleep()函数
  • 如何使用sleep()延迟?
  • 在Python脚本中添加延迟有哪些不同的方法?
  • 使用(Python 3.4或更高版本)提供的asyncio.sleep函数
  • 使用Event().wait方法
  • 使用timer


语法如下:

import time
time.sleep(seconds)


参数:

seconds:我们希望停止执行代码的秒数



示例:在Python中使用sleep()函数

请按照以下给出的步骤在您的python脚本中添加sleep()。

第1步:

import time


步骤2:添加time.sleep()

给sleep()的值是5,希望代码执行在执行时延迟5秒.

time.sleep(5)




下面是一个示例代码:

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”。 调用该函数时,它将执行并在终端内显示消息。

为了增加执行延迟,我们在调用函数之前添加time.sleep()方法。 在执行期间,sleep()将在此处暂停指定的秒数,之后将调用display()函数。


示例如下:

import time

print('Code Execution Started')

def display():
    print('Welcome to Guru99 Tutorials')
    time.sleep(5)

display()
print('Function Execution Delayed')

输出:

G
u
r
u
9
9



在Python脚本中添加延迟有哪些不同的方法?

使用sleep()函数

前面我们已经看到有关如何使用time.sleep()的示例。 让我们在这里使用time.sleep()尝试另一个示例

示例如下:

使用for循环

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(),它显示一条消息“ Welcome to Guru99 tutorials”。 函数async和await中使用了两个关键字。 在函数定义的开头添加async关键字,并在asyncio.sleep()之前添加await。 关键字async / await均用于处理异步任务。

当调用函数display()并遇到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

Timer是线程处理中可用的另一种方法,可以实现与sleep相同的功能。 下例显示了Timer的工作原理:

Timer将输入内容作为延迟时间(以秒为单位)以及需要启动的任务。 要使Timer正常工作,我们需要调用start()方法。 在代码中,给了Timer 5秒钟,完成5秒钟后必须调用功能显示。 当调用Timer.start()方法时,Timer将开始工作。

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()函数将暂停或延迟执行代码,直到执行sleep()输入的秒数。 sleep()函数是time模块的一部分。
  • 当我们要暂时执行代码时,可以使用sleep()函数。 例如正在等待其他过程完成或文件上传等。
  • 除了sleep()方法以外,还有其他方法可以实现代码延迟,比如使用asyncio.sleep,Event().wait和Timer等方法。
  • 与sleep()方法类似,也可以使用asyncio.sleep()方法,不过需要python版本为3.4及更高版本。 要使用asyncio.sleep()方法,需要向函数添加async 和await方法.
  • Event().wait方法来自线程模块。 Event.wait()方法将停止任何进程的执行,直到它花了多少秒作为参数。
  • Timer是线程处理中使用的另一种方法,可以实现与sleep相同的功能。


希望这篇文章对你们有用,

欢迎在下方讨论留言,

谢谢关注.

最近发表
标签列表