kaidraco/example2
0
1import os2from threading import Thread3 4 5class RemoteCommands:6 def __init__(self):7 self.results = {}8 self.pending = []9 self.commands = {}10 11 def _worker_thread(self, key, command):12 self.commands[key] = command13 self.pending.append(key)14 self.results[key] = os.system(command)15 self.pending.remove(key)16 17 def create_thread(self, key, command):18 if (key in self.results) or (key in self.pending):19 raise SystemError(f"{key} is already exists.")20 21 Thread(target=self._worker_thread, args=(key, command), daemon=True).start()22 23 return f"thread {key} is running"24 25 def get_results(self, key):26 status = "not_exists"27 28 if key in self.pending:29 status = "pending"30 elif key in self.results:31 status = "completed"32 33 return {34 "status": status,35 "cmd": self.commands.get(key),36 "code": self.results.get(key)37 }38 