需求:目前有一个值班,分为 3 班,计划做成如下文件
2022-10-24,a,b,c #a b c 代表早中晚班的人名
2022-10-21,c,y,d
2022-10-22,c,d,ra
2022-10-23,c,d,rs
2022-10-24,c,d,rs
2022-10-25,c,d,rs
使用 python 解析文件,定点调用 api 发送明天的值班人,因为一次只能出两个礼拜的值班表,所以值班表文件每两个周要变动一次,计划在值班表修改的时候能够重新读取值班表,让程序可以通过新值班表定点发送。
目前的想法是起一个线程,用 watchdog 监控文件修改,在 on_modified 触发时调用解析函数 Reminder.callback()重新解析配置文件,使得新值班表进入变量内
在 callback 中读取配置文件并解析,通过 datetime.today()获取当前时间,如果当前时间减去设置好的时间 +6h +12h +18h 则调用Reminder.send()发送消息
但目前因为要持续获取当前时间,设置了死循环,导致文件修改时触发回调没用,死循环所在的线程还在运行。 有什么好办法可以解决吗
class MonitorHandler(FileSystemEventHandler):
    def __init__(self) -> None:
        super().__init__()
    ...
    def on_modified(self, event):
        print("on_modified", event.src_path)
        # print(event.src_path[-len(Config.monitoring_file):])
        if Config.monitoring_file == event.src_path[-len(Config.monitoring_file):]:
            print("reload")
            self.reload_file()
   ...
    def reload_file(self):
        Reminder.callback()
Reminder 类:
    def callback():
        with open(Config.monitoring_directory+'/'+Config.monitoring_file, 'r') as f:
            lines = f.readlines()
            dates = []
            dutys = []
            for line in lines:
                l = line.strip('\n').split(',')
                dates.append(l[0])
                dutys.append(l[1:])
            # print(dutys)
            # print()
            while True:
                today = datetime.today()
                zeroToday = today - timedelta(
                    hours=today.hour, minutes=today.minute, seconds=today.second, microseconds=today.microsecond)
                tomorrow = today + timedelta(days=1)
                if str(tomorrow)[:10] in dates:
                    # 取出明天的值班人,和 index
                    index = dates.index(str(tomorrow)[:10])
                    time_index = 0
                    now = datetime.today()
                    while(str(now)[:10] == str(today)[:10]):
                        now = datetime.today()
                        gap = now - zeroToday
                        gap_list = str(gap).split(':')
                        if int(gap_list[0]) == Config.notify_time[time_index]:
                            print("yes")
                            time_index += 1
                        else:
                            print("gap:", gap_list[0],
                                  str(Config.notify_time[time_index]))
                        time.sleep(1)
                else:
                    # Reminder.send("暂无" + str(tomorrow)[:10])
                    break
|  |      1geeglo      2022-10-24 15:11:00 +08:00 一个 crontab 就搞定的事情,搞这么复杂干啥。 | 
|  |      2proxytoworld OP @geeglo 有道理。。是我想多了 |