tornado PeriodicCallback 实现文件更新监听功能

import os

from tornado.ioloop import PeriodicCallback
watch = {}
# 通过读取文件的更新时间
def check_file(modify_times, path) -> None:
    print('监听更新')
    try:
        modified = os.stat(path).st_mtime
    except Exception:
        return
    if path not in modify_times:
        modify_times[path] = modified
        return
    if modify_times[path] != modified:
        print("%s 被更新!"% path)
        modify_times[path] = modified
from tornado import ioloop
# 周期任务,每隔2秒执行一次,向上随机波动2000*0.1=0.2毫秒
PeriodicCallback(lambda :check_file(watch,'test.txt'), callback_time=2000, jitter=0.1).start()
ioloop.IOLoop.instance().start()

你可能感兴趣的:(tornado)