-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
34 lines (32 loc) · 1.03 KB
/
config.py
File metadata and controls
34 lines (32 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import json
import logging
import os
import threading
import time
from helpers import *
# ----------------------------
# Config Loader Task
# ----------------------------
class ConfigTask(threading.Thread):
def __init__(self, path, shared, lock, interval=0.5):
super().__init__(daemon=True)
self.path = path
self.shared = shared
self.lock = lock
self.interval = interval
self._last_mtime = 0
def run(self):
while True:
try:
mtime = os.path.getmtime(self.path)
if mtime != self._last_mtime:
with open(self.path, 'r') as f:
cfg = json.load(f)
with self.lock:
self.shared.clear()
self.shared.update(cfg)
self._last_mtime = mtime
logging.info("Config reloaded.")
except FileNotFoundError:
logging.warning("Settings.json not found.")
time.sleep(self.interval)