def follow_config(path, poll_interval=1.0, force_interval=30.0):
last_reload = -float("inf")
last_mtime = None
last_error_msg = None
abspath = os.path.abspath(path)
while True:
now = time.time()
if now < last_reload:
last_reload = now
mtime = os.path.getmtime(abspath)
if now > last_reload + force_interval or last_mtime != mtime:
try:
configs = load_configs(abspath)
except Exception:
_, exc_value, exc_tb = sys.exc_info()
stack = traceback.extract_tb(exc_tb)
stack = stack[1:] # Make the traceback flatter by discarding the current stack frame
error_msg = "Could not load {path!r} (most recent call last):\n{stack}\n{exception}".format(
path=abspath,
stack="".join(traceback.format_list(stack)).rstrip(),
exception=utils.format_exception(exc_value)
)
if error_msg != last_error_msg:
yield idiokit.send(False, error_msg)
last_error_msg = error_msg
last_mtime = None
else:
yield idiokit.send(True, configs)
last_error_msg = None
last_mtime = mtime
last_reload = now
yield idiokit.sleep(poll_interval)
评论列表
文章目录