📜  Python中的 time.sleep()

📅  最后修改于: 2022-05-13 01:55:07.960000             🧑  作者: Mango

Python中的 time.sleep()

Python time sleep()函数在给定的秒数内暂停执行。

有时,需要停止程序的流程,以便可以进行其他几次执行,或者仅仅是由于所需的实用程序。在这种情况下,sleep() 可以派上用场,它提供了一种准确灵活的方式来在任何时间段内停止代码流。这个函数讨论了这个函数的洞察力。

Python时间 sleep() 语法:

Python时间 sleep() 方法示例

示例 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())


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)


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


Python3
# importing time package
import time
 
# creating a time delay of 4 seconds
time.sleep(4)
 
# creating and Initializing 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)


Python3
# importing time package
import time
 
# creating and Initializing 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)


Python3
# importing time package
import time
 
# creating and Initializing 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]


Python3
# importing time package
import time
 
# creating and Initializing 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)


输出:

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:演示Python time.sleep(1)的应用

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

注意:可以在本地编辑器中看到 sleep() 的可见效果。

示例 3:列表中创建时间延迟

Python3

# importing time package
import time
 
# creating a time delay of 5 seconds
time.sleep(5)
 
# creating and Initializing 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 Initializing 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 Initializing 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 Initializing 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:在 List 中创建3 分钟的时间延迟

Python3

# importing time package
import time
 
# creating and Initializing 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']