Timeloop是一个库,可用于运行多周期任务。这是一个简单的库,使用decorator模式在线程中运行标记函数。

首先安装timeloop库:

pip install timeloop

python定时任务timeloop库用法实例详解

示例代码:

from datetime import datetime, timedelta
from timeloop import Timeloop
 
tl = Timeloop()
 
def task():
    now = datetime.now()
    ts = now.strftime("%Y-%m-%d %H:%M:%S")
    print(ts + '333!')
 
def task2():
    now = datetime.now()
    ts = now.strftime("%Y-%m-%d %H:%M:%S")
    print(ts + "555555!")
 
@tl.job(interval=timedelta(seconds=2))
def sample_job_every_2s():
    task()
 
@tl.job(interval=timedelta(seconds=5))
def sample_job_every_5s():
    task2()

总结

原文地址:https://blog.csdn.net/weixin_44799217/article/details/127353838

相关文章:

  • 2021-08-31
  • 2022-12-23
  • 2022-02-12
  • 2023-03-27
  • 2022-01-20
  • 2021-12-26
猜你喜欢
  • 2022-12-23
  • 2023-03-13
  • 2023-03-13
  • 2022-12-23
  • 2021-12-20
相关资源
相似解决方案