简单的Python定时后台任务

最近碰到个事情需要在flask启动的时候定时清理一些数据, 以前都是用的apscheduler, 但是这次的任务比较简单,定时上报一些请求信息, 不太想通过第三方包的方式进行操作, 看了一下threading源码,在提供Thread主函数的同时, 还提供了一个Timer的类,接下来就围绕这个Timer来说明如何启动轻量定时任务
在这里插入图片描述

Timer官方的说明:

class Timer(Thread):
"""Call a function after a specified number of seconds:
       t = Timer(30.0, f, args=None, kwargs=None)
       t.start()
       t.cancel()     # stop the timer's action if it's still waiting
"""

大概意思就是, 函数会在你设定的时间后执行, 注意这里是只执行一次,所以没有达到我们想要的定时执行,我们结合flask server来演示下如何定时执行。

代码

import datetime
from flask import Flask
import threading

app = Flask(__name__)


def loop(msg):
    print(msg)
    timer = threading.Timer(3, loop, (f"start check: {datetime.datetime.now()}",))
    timer.daemon = True
    timer.start()


@app.route("/")
def test():
   print("hello world")


if __name__ == '__main__':
    loop(f"start check: {datetime.datetime.now()}")
    app.run()

结果

start check: 2022-06-15 10:31:56.069280
 * Serving Flask app 'test' (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
start check: 2022-06-15 10:31:56.069321
start check: 2022-06-15 10:31:59.071218
start check: 2022-06-15 10:32:02.071914
start check: 2022-06-15 10:32:05.074362
start check: 2022-06-15 10:32:08.079799
start check: 2022-06-15 10:32:11.082420
start check: 2022-06-15 10:32:14.082753

存在的问题
当任务在定时时间内不能结束,那么在很短的时间内造成线程堆积,导致程序终止,所以我们进行改进。

改进

import datetime
import time
from flask import Flask
import threading

app = Flask(__name__)


def task():
    print(f"start check: {datetime.datetime.now()}")


def loop():
    # 使用while True的方式保持这个Timer线程不停止
    while True:
        task()
        time.sleep(3)


@app.route("/")
def test():
    print("hello world")


if __name__ == '__main__':
    timer = threading.Timer(3, loop)
    timer.daemon = True
    timer.start()
    app.run()

使用while True的方式保持这个Timer线程不停止, 这样就只会有一个线程在后台执行,也不会造成线程堆积最终程序退出。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值