Python 中 6种实现 时间延迟(sleep)/等待(wait)功能的方法 – 码微

您所在的位置:网站首页 肠胃炎的医院证明图片 Python 中 6种实现 时间延迟(sleep)/等待(wait)功能的方法 – 码微

Python 中 6种实现 时间延迟(sleep)/等待(wait)功能的方法 – 码微

2024-04-30 03:14| 来源: 网络整理| 查看: 265

Python 延迟/等待

如何在 Python 脚本中设置时间延迟?比如让当前的代码延迟50毫秒后再执行?

下面个大家分享6种方法,有疑问可以留言一起探讨

使用time.sleep()

time模块的sleep()方法采用浮点参数,支持亚秒级分辨率,sleep()实际上暂停了操作系统调用它的线程的处理,允许其他线程和进程在它休眠时执行

import time print('Hello') time.sleep(5) # Number of seconds print('Bye')Code language: PHP (php) pygame.time.wait() import pygame # If you are going to use the time module # don't do "from pygame import *" pygame.init() print('Hello') pygame.time.wait(5000) # Milliseconds print('Bye')Code language: PHP (php) pyplot.pause()

matplotlib 的函数pyplot.pause()示例:

import matplotlib print('Hello') matplotlib.pyplot.pause(5) # Seconds print('Bye')Code language: PHP (php) .after()方法(与Tkinter配合使用): import tkinter as tk # Tkinter for Python;2 root = tk.Tk() print('Hello') def ohhi(): print('Oh, hi!') root.after(5000, ohhi) # Milliseconds and then a function print('Bye')Code language: PHP (php) asyncio.sleep()方法(必须在异步循环中): await asyncio.sleep(5)Code language: JavaScript (javascript) selenium中的时间延迟/等待操作

driver.implicitly_wait(5)或者如果必须等到特定操作完成或找到元素时,下面的方法更有用:

self.wait.until(EC.presence_of_element_located((By.ID, 'UserName'))Code language: PHP (php)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3