python定时器

yizhihongxing

一.Timer

Timer为threading中的一个类,用来指定的秒数后调用函数,我们来看下Timer类的构造参数。

python定时器

interval:设置定时运行的时间

function:设置定时的事件

args:参数

kwargs:字典类型的参数

下面我们看下具体的用法

1.无参的情况下

from threading import Timer
def fun():
    print("我是一个定时器")

t = Timer(3, fun)  # 声明一个定时器,设置多少3s后执行
t.start()  # 启动定时器

2.有参的情况下

from threading import Timer

def fun(name):
    print("我是一个定时器", name)

t = Timer(3, fun, args=("黎明",))  # 声明一个定时器,设置多少s后执行
t.start()  # 启动定时器

3.多参的情况下

from threading import Timer

def fun(name, age):
    print("我是一个定时器", name, age)

t = Timer(3, fun, args=("黎明", 1))  # 声明一个定时器,设置多少s后执行
t.start()  # 启动定时器

4.要想设置每隔3s运行一次

from threading import Timer

a = 0

def fun():
    global a
    a = a + 1
    print("我是一个定时器")
    t = Timer(3, fun)  # 声明一个定时器,设置多少s后执行
    t.start()  # 启动定时器
    if a == 3:
        t.cancel()  # 取消定时器

fun()

start():启动定时任务

cancel():取消定时任务

二.schedule

schedule是一个轻量级的定时库,可指定某秒/分/时/天/周的时间。

1.因为schedule是第三方库,使用时需提前安装

pip install schedule

2.设置格式

scedule.every(时间数).时间类型.do(job):表单个时间

scedule.every(时间数1).to(时间数2).时间类型.do(job):表一段时间范围时间

3.具体用法

设置每隔5秒后运行一次

import schedule

def fun(name, age):
    print("我是一个定时器", name, age)

schedule.every(5).seconds.do(fun, "李明", 12)  # 每隔5s运行一次

while True:
    schedule.run_pending()  # 运行所有可以运行的任务

设置每隔1-5秒运行一次

import schedule

def fun(name, age):
    print("我是一个定时器", name, age)

schedule.every(1).to(5).seconds.do(fun, "李明", 12)  # 每隔1-5s运行一次

while True:
    schedule.run_pending()  # 运行所有可以运行的任务

下面设置分/时/天/周就不一一介绍了,直接贴写法了

schedule.every(2).minutes.do(fun, "李明", 12)  # 每2min运行一次
schedule.every(2).minutes.at(":20").do(fun, "李明", 12)  # 每2min30s运行一次
schedule.every(2).to(2).minutes.do(fun, "李明", 12)  # 每2-5min运行一次
schedule.every().hour.do(fun, "李明", 12)  # 每2小时执行一次
schedule.every().hour.at(":40").do(fun, "李明", 12)  # 每小时40分钟执行一次
schedule.every().day.do(fun, "李明", 12)  # 每天当前时间执行一次
schedule.every().day.at("15:15").do(fun, "李明", 12)  # 每天规定时间执行一次,
schedule.every().tuesday.at("15:19:30").do(fun, "李明", 12)  # 每周二15:19:30运行一次
schedule.every().tuesday.do(fun, "李明", 12)  # 每周二当前时间运行一次

周一至周五写法

monday

tuesday

wednesday

thursday

friday

saturday

sunday

4.取消定时任务

clear():默认取消全部任务,也可通过tag取消指定的任务

import schedule


def fun(name, age):
    print("我是一个定时器", name, age)


schedule.every(1).to(5).seconds.do(fun, "李明", 14).tag("tag")  # 每隔1-5s运行一次,设置tag

while True:
    schedule.run_pending()  # 运行所有可以运行的任务
    schedule.clear("tag")  # 取消指定标签的任务
    # schedule.clear()  # 取消全部任务

 

 

 

 

 

文章来源:https://www.cnblogs.com/lihongtaoya/ ,请勿转载

 

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:python定时器 - Python技术站

(0)
上一篇 2023年3月31日 下午9:01
下一篇 2023年3月31日 下午9:01

相关文章

  • python pyinstaller库

    简要 pyinstaller模块主要用于python代码打包成exe程序直接使用,这样在其它电脑上即使没有python环境也是可以运行的。 用法 一.安装 pyinstaller属于第三方库,因此在使用的时候需提前安装 pip install pyinstaller 二.配置spec文件 1.配置生成exe程序文件夹 (1)如果不熟悉spec配置内容,可以在…

    python 2023年4月25日
    00
  • 自动化测试报告(allure/html)

    pytest有两种生成测试报告的方法(html和allure),今天就给大家一一介绍下 html 一.pytest-html基本语法 1.安装:pip install pytest-html 2.查看版本:pip show pytest-html 3.生成测试报告基本语法: 语法一:pytest –html=生成报告的url  运行用例的.py文件 语法二…

    2023年4月2日
    00
  • selenium基本用法

    一.元素操作 send_keys:赋值 clear:清空 click:点击 from selenium import webdriver from selenium.webdriver.common.by import By driver = webdriver.Chrome() driver.get(“https://www.baidu.com”) dri…

    2023年4月2日
    00
  • 详解数据驱动

    本章主要介绍pytest几种数据驱动的方法,也是我们做接口自动化中经常要使用到的,大致分为以下四种。  一.yaml  二.json  三.csv  四.excel     一.yaml 1.安装插件:pip install pyyaml 2.yaml的两种读写方法load()和dump(),话不多说,直接上代码   load()为读取json流,读取是加上…

    2023年4月2日
    00
  • python pyinstaller库

    简要 pyinstaller模块主要用于python代码打包成exe程序直接使用,这样在其它电脑上即使没有python环境也是可以运行的。 用法 一.安装 pyinstaller属于第三方库,因此在使用的时候需提前安装 pip install pyinstaller 二.配置spec文件 1.配置生成exe程序文件夹 (1)如果不熟悉spec配置内容,可以在…

    python 2023年4月25日
    00
合作推广
合作推广
分享本页
返回顶部