MCPcopy Create free account
hub / github.com/nat/openplayground / MonitorThread

Class MonitorThread

server/app.py:79–135  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

77 yield
78
79class MonitorThread(threading.Thread):
80 def __init__(self, model, output_buffer):
81 super().__init__()
82 self.model = model
83 self.output_buffer = output_buffer
84 self._stop_event = threading.Event()
85 self.event_emitter = EventEmitter()
86
87 def run(self):
88 with redirect_stderr(self.output_buffer):
89 # Code that may generate errors goes here
90 output_buffer = self.output_buffer
91 current_shard = 0
92 total_shards = 0
93 last_line = 0
94
95 while not self._stop_event.is_set():
96 try:
97 lines = output_buffer.getvalue().splitlines()[last_line:]
98 last_line += len(lines)
99
100 for line in lines:
101 if line == "":
102 continue
103
104 if line.startswith("Downloading shards:"):
105 if progress := re.search(r"\| (\d+)/(\d+) \[", line):
106 current_shard, total_shards = int(progress[1]), int(progress[2])
107 elif line.startswith("Downloading"):
108 logger.info(line)
109 percentage = re.search(r":\s+(\d+)%", line)
110 percentage = percentage[0][2:] if percentage else ""
111
112 progress = re.search(r"\[(.*?)\]", line)
113 if progress and "?" not in progress[0]:
114 current_duration, rest = progress[0][1:-1].split("<")
115 total_duration, speed = rest.split(",")
116
117 if download_size := re.search(r"\| (.*?)\[", line):
118 current_size, total_size = download_size[0][2:-1].strip().split("/")
119
120 self.event_emitter.emit(EVENTS.MODEL_DOWNLOAD_UPDATE, self.model, {
121 'current_shard': current_shard,
122 'total_shards': total_shards,
123 'percentage': percentage.strip(),
124 'current_duration': current_duration,
125 'total_duration': total_duration,
126 'speed': speed.strip(),
127 'current_size': current_size,
128 'total_size': total_size,
129 })
130 except Exception as e:
131 logger.info(f"""[ERROR] {str(e)}""")
132 time.sleep(0.5)
133
134 def stop(self):
135 self._stop_event.set()
136

Callers 1

__download_loop__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected