📜  如何在Python中添加时间延迟?

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

如何在Python中添加时间延迟?

在本文中,我们将讨论如何在我们的程序代码中引入或添加时间延迟。

时间延迟

  • 为了在我们的程序代码中添加时间延迟,我们使用了 time 模块中的 sleep()函数。这是我们不需要外部安装的Python内置模块。
  • 时间延迟意味着我们在程序代码的执行时间内添加延迟。根据您的要求,它应该在两个语句之间或程序代码的任何部分之间。

句法:

time.sleep(value)

方法:

  • 导入时间模块
  • 为了在执行期间添加时间延迟,我们在我们想要延迟的两个语句之间使用 sleep()函数。在 sleep()函数中,将参数作为整数或浮点值传递。
  • 运行程序。
  • 注意执行时间的延迟。

完美地理解主题。让我们通过一些例子来看看实现。

注意:作为输出,我显示了 GIF,以便您可以注意到程序代码在执行期间的时间延迟。

示例 1:通过添加时间延迟打印数字。

Python3
# importing module
import time
  
  
# running loop from 0 to 4
for i in range(0,5):
    
  # printing numbers
  print(i)
    
  # adding 2 seconds time delay
  time.sleep(2)


Python3
# importing time module
import time
  
  
def message(string):
    
    for i in string:
        
        # printing each character of the message
        print(i, end="")
          
        # adding time delay of half second
        time.sleep(0.5)
  
  
# main function
if __name__ == '__main__':
    msg = "Its looks like auto typing"
      
    # calling the function for printing the 
    # characters with delay
    message(msg)


Python3
# importing module
import time
  
  
# function to print the pattern
def pattern(n):
    
    for i in range(0, n):
        for j in range(0, i+1):
            
            print('*', end=' ')
              
            # adding two second of time delay
            time.sleep(0.5)
        print(' ')
  
  
# main function
if __name__ == '__main__':
    
    # taking range from the user
    num = 4
    print("Printing the pattern")
      
    # calling function to print the pattern
    pattern(num)


Python3
# importing
import time
from threading import Thread
  
  
# making first thread of Geeks
class Geeks(Thread):
    
    def run(self):
        for x in range(4):
            print("Geeks")
              
            # adding delay of 2.2 seconds
            time.sleep(2.2)
  
# making second thread of For
class For(Thread):
    
    def run(self):
        for x in range(3):
            print('For')
              
            # adding delay of 2.3 seconds
            time.sleep(2.3)
  
  
print("Hello")
  
# making the object for both the 
# threads separately
g1 = Geeks()
f1 = For()
  
# starting the first thread
g1.start()
  
# starting the second thread
f1.start()
  
# waiting for the both thread to join
# after completing their job
g1.join()
f1.join()
  
# when threads complete their jobs
# message will be printed
print("All Done!!")


输出:

示例 2:使用 sleep() 对每个字符进行戏剧性打印。

蟒蛇3

# importing time module
import time
  
  
def message(string):
    
    for i in string:
        
        # printing each character of the message
        print(i, end="")
          
        # adding time delay of half second
        time.sleep(0.5)
  
  
# main function
if __name__ == '__main__':
    msg = "Its looks like auto typing"
      
    # calling the function for printing the 
    # characters with delay
    message(msg)

输出:

示例3:通过从用户获取范围并添加时间延迟来打印图案。

蟒蛇3

# importing module
import time
  
  
# function to print the pattern
def pattern(n):
    
    for i in range(0, n):
        for j in range(0, i+1):
            
            print('*', end=' ')
              
            # adding two second of time delay
            time.sleep(0.5)
        print(' ')
  
  
# main function
if __name__ == '__main__':
    
    # taking range from the user
    num = 4
    print("Printing the pattern")
      
    # calling function to print the pattern
    pattern(num)

输出:

示例 4:使用 sleep() 进行多线程处理

蟒蛇3

# importing
import time
from threading import Thread
  
  
# making first thread of Geeks
class Geeks(Thread):
    
    def run(self):
        for x in range(4):
            print("Geeks")
              
            # adding delay of 2.2 seconds
            time.sleep(2.2)
  
# making second thread of For
class For(Thread):
    
    def run(self):
        for x in range(3):
            print('For')
              
            # adding delay of 2.3 seconds
            time.sleep(2.3)
  
  
print("Hello")
  
# making the object for both the 
# threads separately
g1 = Geeks()
f1 = For()
  
# starting the first thread
g1.start()
  
# starting the second thread
f1.start()
  
# waiting for the both thread to join
# after completing their job
g1.join()
f1.join()
  
# when threads complete their jobs
# message will be printed
print("All Done!!")

输出: