python睡眠

Hello everyone, hope you are learning python well. In this tutorial we will learn about python time sleep() method. Python sleep function belongs to the python time module that is already discussed earlier

大家好,希望您对python学习得很好。 在本教程中,我们将学习python time sleep()方法。 Python sleep函数属于前面已经讨论过的python time模块

Python时间睡眠 (Python time sleep)

Python time sleep function is used to add delay in the execution of a program. We can use python sleep function to halt the execution of the program for given time in seconds. Notice that python time sleep function actually stops the execution of current thread only, not the whole program.

Python时间睡眠功能用于增加程序执行的延迟。 我们可以使用python sleep函数在给定的时间(以秒为单位)中暂停程序的执行。 注意,python time sleep函数实际上仅停止当前线程的执行,而不是整个程序的执行。

Python time sleep()函数语法 (Python time sleep() function syntax)

Python sleep() is a method of python time module. So, first we have to import the time module then we can use this method. Way of using python sleep() function is:

Python sleep()是python时间模块的一种方法。 因此,首先我们必须导入时间模块,然后才能使用此方法。 使用python sleep()函数的方式是:

Here the argument of the sleep() method t is in seconds. That means, when the statement time.sleep(t) is executed then the next line of code will be executed after t seconds. See the following example:

这里的sleep()方法t的参数以秒为单位。 这意味着,当执行语句time.sleep(t)时,下一行代码将在t秒后执行。 请参见以下示例:

# importing time module
import timeprint("Before the sleep statement")
time.sleep(5)
print("After the sleep statement")

If you run the above code then you will see that the second print executes after 5 seconds. So you can make delay in your code as necessary.

如果运行上面的代码,您将看到5秒钟后执行第二次打印。 因此,您可以根据需要延迟代码。

The argument can be in floating value also to have more precise delay. For example you want to make delay for 100 milliseconds which is 0.1 seconds, as following:

该参数可以为浮点值,也可以具有更精确的延迟。 例如,您要延迟100毫秒(即0.1秒),如下所示:

import time
time.sleep(0.100)

Python睡眠示例 (Python sleep example)

Let’s see the following example of python time sleep function.

让我们看下面的python time sleep函数示例。

import time
startTime = time.time()
for i in range(0,5):print(i)# making delay for 1 secondtime.sleep(1)
endTime = time.time()
elapsedTime = endTime - startTime
print("Elapsed Time = %s" % elapsedTime)

This will output:

这将输出:

0
1
2
3
4
Elapsed Time = 5.059988975524902

Elapsed time is greater than 5 because each time in the for loop, execution is halted for 1 second. The extra time is because of the execution time of the program, operating system thread scheduling etc.

经过的时间大于5,因为每次在for循环中,执行都会暂停1秒钟。 额外的时间是由于程序的执行时间,操作系统线程调度等。

python sleep()的不同延迟时间 (Different delay time of python sleep())

Sometimes you may need to delay for different seconds of time. You can do it as follows:

有时您可能需要延迟不同的时间。 您可以按照以下步骤进行操作:

import timefor i in [ .5, .1, 1, 2]:print("Waiting for %s" % i , end='')print(" seconds")time.sleep(i)

This will output:

这将输出:

Waiting for 0.5 seconds
Waiting for 0.1 seconds
Waiting for 1 seconds
Waiting for 2 seconds

使用sleep()进行惊人的打印 (Dramatic printing using sleep())

You may need to print some message in a dramatic way, you can do it as following:

您可能需要以戏剧性的方式打印一些消息,可以按照以下步骤进行操作:

# importing time module
import time
message = "Hi!!! I am trying to create suspense"for i in message:# printing each character of the messageprint(i)time.sleep(0.3)

If you run the above code then, you will see that after printing every character of the message it’s taking some time, which seems like dramatic.

如果运行上面的代码,您将看到在打印消息的每个字符之后需要花费一些时间,这似乎很麻烦。

Python线程睡眠 (Python thread sleep)

Python time sleep() function is very important method for multithreading. Below is a simple example showing that the python time sleep function halts the execution of current thread only in multithreaded programming.

Python time sleep()函数是用于多线程的非常重要的方法。 下面是一个简单的示例,显示python time sleep函数仅在多线程编程中才停止当前线程的执行。

import time
from threading import Threadclass Worker(Thread):def run(self):for x in range(0, 11):print(x)time.sleep(1)class Waiter(Thread):def run(self):for x in range(100, 103):print(x)time.sleep(5)print("Staring Worker Thread")
Worker().start()
print("Starting Waiter Thread")
Waiter().start()
print("Done")

Below image shows the output produced by above python thread sleep example.

下图显示了以上python线程睡眠示例产生的输出。

From the output it’s very clear that only the threads are being stopped from execution and not the whole program by python time sleep function.

从输出中很明显,通过python time sleep函数,只有线程正在停止执行,而不是整个程序。

That’s all about python time sleep function or python sleep function.

这就是关于python time sleep函数或python sleep函数的全部内容。

Reference: StackOverFlow Post, API Doc

参考: StackOverFlow Post , API文档

翻译自: https://www.journaldev.com/15797/python-time-sleep

python睡眠

python睡眠_Python时间睡眠()相关推荐

  1. python测量血压_Python时间性能测量

    主要有以下三种方式: 一,CPU时间 time.clock() 测量CPU时间,比较精准,通过比较程序运行前后的CPU时间差,得出程序运行的CPU时间. 二, 时钟时间 time.time() 测量时 ...

  2. 睡眠伤害计算机硬件吗,电脑长时间睡眠、不关机对电脑有伤害吗?

    原标题:电脑长时间睡眠.不关机对电脑有伤害吗? 12月23日消息,英特尔中国官方微博今天科普的问题是:电脑长时间睡眠,不关机对电脑有伤害吗? 英特尔官微表示:影响还是会有的,因为长时间睡眠状态不关机, ...

  3. go time.Sleep睡眠指定时间(小时级到纳秒级)

    go用来指定睡眠时间的函数为time.Sleep,接口为: // Sleep pauses the current goroutine for at least the duration d. // ...

  4. python时间序列函数_python时间日期函数与利用pandas进行时间序列处理详解

    python标准库包含于日期(date)和时间(time)数据的数据类型,datetime.time以及calendar模块会被经常用到. datetime以毫秒形式存储日期和时间,datetime. ...

  5. python获取系统时间函数_Python常用时间操作总结【取得当前时间、时间函数、应用等】...

    本文实例讲述了Python常用时间操作.分享给大家供大家参考,具体如下: 我们先导入必须用到的一个module >>> import time 设置一个时间的格式,下面会用到 > ...

  6. python日期函数_python 时间及日期函数

    本人最近新学python ,用到关于时间和日期的函数,经过一番研究,从网上查找资料,经过测试,总结了一下相关的方法. import time import datetime '''时间转化为时间戳: ...

  7. python日期函数_python 时间相关函数

    python 中与时间处理相关的模块包括 time.datetime.以及 calendar time 模块 time() 函数:time() 函数用于返回当前时间的时间戳(1970年01月08时00 ...

  8. python现在的时间是几点_Python 的日期和时间处理

    来源:开源最前线  ID:OpenSourceTop 写过Python程序的人都知道,Python日期和时间的处理非常繁琐和麻烦. 除了将字符串转换为更有用的 Python 对象之外,还有许多库具有一 ...

  9. python获取网络时间_python获取网络时间和本地时间

    今天我们来看一下如何用python获取网络时间和本地时间,直接上代码吧,代码中都有注释. python获取网络时间获取网络时间 def getBeijinTime(): ""&qu ...

最新文章

  1. 第七篇:循环神经网络
  2. 2017北京云栖大会:云效企业级协同研发专场议题揭秘!
  3. python多线程之threading
  4. Python 文件读取与写入操作方法
  5. WPF的转换器中使用Brush应注意问题
  6. matlab自带kpca,求对矩阵进行PCA或者KPCA特征提取的matlab代码
  7. ubuntu 软件包管理工具 dpkg,apt-get,aptitude 区别
  8. #从零开始学Swift2.0# No.4 枚举, 元组, 数组和字典
  9. 49 - 算法 - Leetcode-111 -二叉树的最小深度 -递归循环
  10. 自学python好找工作么-学完Python好找工作吗?为什么有人学完找不到工作?
  11. 仿淘宝首页产品分类菜单栏的设计
  12. cityscape 数据集 mmsegmentation训练记录
  13. PC系统启动过程简介以及Windows引导修复
  14. STK12已出,STK 12 新特性介绍
  15. 【180929】数字拼图游戏源码
  16. Spring Security系列教程11--Spring Security核心API讲解
  17. coloros系统和android系统,最流畅的安卓操作系统?国产两大系统对比,绿厂凭借细节取胜...
  18. 吹爆“Alibaba”自研Spring全家桶全套全彩学习笔记(终极版)
  19. CMMI的五个等级是什么?
  20. java模拟生存RPG游戏开发

热门文章

  1. 数据结构之散列表实现
  2. nodejs初探(四)实现一个多人聊天室
  3. [转载] Python—urllib模块
  4. Django-manage.py
  5. 高通HAL层之Sensor HAL
  6. Mybatis一对一和一对多配置
  7. laravel5.3-数据库操作下的局部or条件与全局or条件(orWhere的局部与全局)
  8. Linux中的nc测试端口是否开放
  9. [转载] su和sudo
  10. const限定符用法汇总