import threading import os import json class Worker(threading.Thread): def __init__(self, directory, **kwargs): self.directory = directory self.status = { "state": "idle", "directory": directory, } kwargs["daemon"] = True return super().__init__(**kwargs) def workdir(self, *path): return os.path.join(self.directory, *path) def write_state(self, subdir, state): with open(self.workdir(subdir, "state.json"), "w") as f: json.dump(f, state) def read_state(self, subdir): with open(self.workdir(subdir, "state.json")) as f: return json.load(f)