如何使用Python sleep()函数?

While developing applications we may need some rest for the execution. Python provides sleep() function for different use cases in order to delay program execution. In this tutorial, we will examine Python sleep() function for different conditions.

在开发应用程序时,我们可能需要一些休息才能执行。 Python为不同的用例提供了sleep()函数,以延迟程序执行。 在本教程中,我们将针对不同的条件检查Python sleep()函数。

睡眠时间精度 (Sleep Time Precision)

Before starting a tutorial we should know that Python is an interpreted language and is not real-time. So while using python there will be some inconsistencies. While using sleep function Linux can use 1-millisecond precision but Windows has 13 milliseconds precision. Keep in mind that if you will develop time precision applications.

在开始教程之前,我们应该知道Python是一种解释性语言,不是实时的。 因此,在使用python时会有一些不一致之处。 使用睡眠功能时,Linux可以使用1毫秒的精度,而Windows可以使用13毫秒的精度。 请记住,如果要开发时间精确度应用程序。

导入时间模块 (Import time Module)

As a beginner, we know that Python provides modules for different functionalities. time module provides the sleep() function. In order to use sleep() function we should import the time library like below.

作为一个初学者,我们知道Python提供了用于不同功能的模块。 time模块提供sleep()函数。 为了使用sleep()函数,我们应该导入time库,如下所示。

import time

sleep()函数 (sleep() Function)

Now we can use sleep() function by providing the time we want to wait. In this example, we will wait for 1 second. As I can not take a screenshot of delay there will be no screenshot ;). sleep() function will suspend the current thread. So if there are other threads they will do not wait.

现在,我们可以通过提供要等待的时间来使用sleep()函数。 在此示例中,我们将等待1秒钟。 由于我无法截取延迟的屏幕截图,因此将没有屏幕截图;)。 sleep()函数将挂起当前线程。 因此,如果还有其他线程,它们将不等待。

time.sleep(1)

睡眠10秒 (Sleep 10 Seconds)

Sleep function will interpret the provided value as second which must be a number. In this example, we will set 10 seconds wait.

睡眠功能会将提供的值解释为秒,该值必须是数字。 在此示例中,我们将设置10秒等待时间。

time.sleep(10)

睡眠数毫秒 (Sleep For Milliseconds)

What if we need to wait for given milliseconds. We can provide a floating number in order to express milliseconds. In this example, the delay will be 500 milliseconds which is expressed as 0.5

如果我们需要等待给定的毫秒数怎么办。 我们可以提供一个浮点数来表示毫秒。 在此示例中,延迟将为500毫秒,表示为0.5

time.sleep(0.5)

Or we can express 500 milliseconds like below.

或者我们可以像下面这样表示500毫秒。

time.sleep(.500)

睡眠指定分钟 (Sleep For Specified Minutes)

What if we do not want to calculate between minutes and seconds. We can use a simple calculation to simplify things. In this example we will wait for 4 minute which is expressed in seconds 4*60

如果我们不想在分钟和秒之间进行计算该怎么办。 我们可以使用简单的计算来简化事情。 在此示例中,我们将等待4分钟,以秒4*60

time.sleep(4*60)
LEARN MORE  Python Date Time Operations Tutorial with Examples
了解更多Python日期时间操作教程及示例

翻译自: https://www.poftut.com/use-python-time-sleep-to-delay-application-execution/

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值