import threading import os import json import logging class Worker(threading.Thread): def __init__(self, directory, **kwargs): self.directory = directory self.status = { "state": "idle", } kwargs["daemon"] = True return super().__init__(**kwargs) def workdir(self, *path): return os.path.join(self.directory, *path) def write_state(self, subdir, state): logging.debug("Writing state: %s" % repr(state)) statefn = self.workdir(subdir, "sucker.json") newstatefn = statefn + ".new" with open(newstatefn, "w") as f: json.dump(state, f) os.rename(newstatefn, statefn) def read_state(self, subdir): with open(self.workdir(subdir, "sucker.json")) as f: return json.load(f) def clear_state(self, subdir): os.unlink(self.workdir(subdir, "sucker.json"))