当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python time.sleep()用法及代码示例


sleep()是在python 3的time()模块中定义的。有时,需要破坏程序的流程,以便可以进行其他几次执行,或者仅仅是由于所需的实用程序。在这种情况下,sleep()可以派上用场,它提供了一种准确而灵活的方式来在任何时间段内停止代码流。此函数讨论此函数的见解。

语法:sleep(sec)参数:sec:要求停止代码的秒数。返回:VOID。


代码1:展示sleep()

Python3

# Python code to demonstrate
# working of sleep()
 
import time
 
# printing the start time 
print("The time of code execution begin is:", end ="")
print(time.ctime())
 
# using sleep() to hault the code execution
time.sleep(6)
 
 
# printing the end time 
print("The time of code execution end is:", end ="")
print(time.ctime())


输出:

The time of code execution begin is:Mon Apr  9 20:57:10 2018
The time of code execution end is:Mon Apr  9 20:57:16 2018


应用:
sleep()用于许多应用程序。如果以固定间隔重复执行后台线程,则可以借助sleep()来实现。另一个流行的应用程序是使用sleep()逐字母打印单词,以获得良好的用户接口。后者由下面的代码表示。

代码2:演示sleep()的应用

Python3

# Python code to demonstrate
# application of sleep()
 
import time
 
# initializing string 
strn = "GeeksforGeeks"
 
# printing geeksforgeeks after delay
# of each character
for i in range(0, len(strn)):
    print(strn[i], end ="")
    time.sleep(2)

输出:

GeeksForGeeks
Note: Visible effect of sleep() can be seen in the local editor.

代码3:在列表中创建时间延迟

Python3

# importing time package
import time
 
# creating a time delay of 5 seconds 
time.sleep(5)
 
# creating and Initilizing a list
myList = ['Jai', 'Shree', 'RAM', 5, 'August', 2020]
 
# the list will be displayed after the delay of 5 seconds
print(myList)

输出:

延迟5秒后,我们将获得以下输出:

['Jai', 'Shree', 'RAM', 5, 'August', 2020]

代码4:在元组中创建时间延迟



Python3

# importing time package
import time
 
# creating a time delay of 4 seconds 
time.sleep(4)
 
# creating and Initilizing a tuple
mytuple = ('Anil Kumbl', 'Sachin Tendulkar', 'Sunil Gavaskar', 'Rahul Dravid', 'Mahendra Singh Dhoni', 
          'Dennis Lillee', 'Muttiah Muralitharan', 'Shane Warne')
 
# the tuple will be displayed after the delay of 4 seconds
print(mytuple)

输出:

延迟4秒后,我们将获得以下输出:

('Anil Kumbl', 'Sachin Tendulkar', 'Sunil Gavaskar', 'Rahul Dravid',
'Mahendra Singh Dhoni', 'Dennis Lillee', 'Muttiah Muralitharan', 'Shane Warne')

代码5:创建多个时间延迟

Python3

# importing time package
import time
 
# creating and Initilizing a list
Languages = ['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']
 
# creating a time delay of 5 seconds
time.sleep(5)
 
# the list will be displayed after the delay of 5 seconds
print(Languages)
 
for lan in Languages:
     
    # creating a time delay of 13 seconds 
    time.sleep(13)
     
    # After every 13 seconds an item of list will be displayed
    print(lan)

输出:

延迟5秒后,列表将显示为:

['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']

然后,每隔13秒,列表项将显示为:

Java
C++
Python
Javascript
C#
C
Kotlin

代码6:列表理解中的时间延迟

Python3

# importing time package
import time
 
# creating and Initilizing a list
cricketers = ['Anil Kumble', 'Sachin Tendulkar', 'Sunil Gavaskar', 'Rahul Dravid', 'Mahendra Singh Dhoni', 
          'Dennis Lillee', 'Muttiah Muralitharan', 'Shane Warne']
 
# time delay of 7 seconds is created 
# after every 7 seconds item of list gets displayed
cricketers = [(time.sleep(7), print(cric)) for cric in cricketers]

输出:

每隔7秒,列表中的项目将显示为:

Anil Kumble
Sachin Tendulkar
Sunil Gavaskar
Rahul Dravid
Mahendra Singh Dhoni
Dennis Lillee
Muttiah Muralitharan
Shane Warne

代码7:在列表中创建3分钟的延迟

Python3

# importing time package
import time
 
# creating and Initilizing a list
Languages = ['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']
 
# creating a time delay of 3 minutes
time.sleep(3 * 60)
 
# the list will be displayed after the delay of 3 minutes
print(Languages)

输出:

延迟3分钟后,列表将显示为:

['Java', 'C++', 'Python', 'Javascript', 'C#', 'C', 'Kotlin']


注:本文由纯净天空筛选整理自manjeet_04大神的英文原创作品 time.sleep() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。