Python Sleep是什么?

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

在本教程中,您将学习:

  • Python Sleep是什么
  • 示例:在Python中使用sleep()函数
  • 如何使用sleep延迟执行功能?
  • 在Python脚本中添加延迟有哪些不同的方法?
  • 使用asyncio.sleep函数
  • 使用Event().wait
  • 使用计时器
  • time.sleep语法

    import time
    time.sleep(seconds)

    seconds :您希望停止执行代码的秒数。

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

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

    import time
    

    步骤2:添加time.sleep()

    作为sleep()输入的数字5是您希望代码执行在执行后暂停的秒数。

    time.sleep(5)
    

    这是一个工作代码以及print()中的消息,以显示执行时终端上消息显示的延迟。

    import time
    print("Welcome to icfedu.cn")
    time.sleep(5)
    print("5秒后打印这条消息")

    Output:

    Welcome to icfedu.cn
    5秒后打印这条消息

    如何使用sleep()延迟执行功能?

    下面显示的示例具有一个定义为display()的函数。display()函数将显示一条消息“ Welcome to icfedu.cn”。调用该函数时,它将执行并在终端内部显示消息。

    为了增加函数执行的延迟,让我们在调用函数之前在Python中添加time.sleep。在执行过程中,Python time.sleep将在该位置暂停数秒,然后将调用display()函数。

    Example:

    import time
    print('代码开始执行')
    def display():
        print('Welcome to icfedu.cn')
        time.sleep(5)
    display()
    print('延迟执行函数')
    

    Output:

    代码开始执行
    Welcome to icfedu.cn
    延迟执行函数

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

    使用sleep()函数

    该代码有一个for循环,它将使用字符串变量,并以1秒的延迟显示每个字符。

    import time
    my_message = "icfedu"
    for i in my_message:
       print(i)
       time.sleep(1)
    

    Output:

    使用asyncio.sleep函数

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

    该脚本具有函数调用display(),该函数显示一条消息“ Welcome to icfedu.cn”。函数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 icfedu.cn')
    asyncio.run(display())
    

    Output:

    Code Execution Started
    Welcome to icfedu.cn

    使用Event().wait

    wait方法来自线程模块。Event.wait()方法将暂停任何进程的执行,直到它花了多少秒作为参数。下面的示例显示了Event的工作方式:

    该代码使用Event().wait(5)。数字5是代码延迟到下一行调用函数display。5秒钟完成后,将调用函数display(),并将消息打印在终端内部。

    from threading import Event
    print('Code Execution Started')
    def display():
        print('Welcome to icfedu.cn')
    Event().wait(5) 
    display()
    

    Output:

    Code Execution Started
    Welcome to icfedu.cn

    使用timer

    Timer是Threading可用的另一种方法,它有助于获得与Python时间睡眠相同的功能。下例显示了计时器的工作原理:

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

    from threading import Timer
    print('Code Execution Started')
    def display():
        print('Welcome to icfedu.cn')
    t = Timer(5, display)  
    t.start()
    

    Output:

    Code Execution Started
    Welcome to icfedu.cn
  • Python sleep函数将暂停Python代码或将程序的执行延迟指定为sleep输入的秒数。sleep函数是Python时间模块的一部分。
  • 当您要暂时停止执行代码时,可以使用Python sleep函数。例如,如果您正在等待其他过程完成或文件上传等。
  • 除了睡眠以外,还有很多方法可以将Python延迟函数添加到代码中,并且它们使用asyncio.sleep,Event。wait和Timer。
  • 与sleep方法类似,存在asyncio.sleep方法,其版本为python 3.4及更高版本。要使用异步睡眠方法,您需要向函数添加异步并等待
  • Event wait方法来自线程模块。Event.wait方法将暂停任何进程的执行,直到它花了多少秒作为参数。
  • 计时器是线程处理中可用的另一种方法,它有助于获得与睡眠相同的功能。
  • Posted in Python

    发表回复 取消回复

    本分类下文章

    Sytem Verilog 教学 (视频) System Verilog 教材 系统验证基础–OVM与UVM 第六节 三极管的特性曲线及参数 第五节 三极管电流分配关系及电流放大系数 第四节 半导体三极管的原理 无线多径衰落系数的统计特性 无线信号多径衰落 无线信号建模–多径效应 第三节 半导体二极管

    分类目录