Spaces:
Running
on
Zero
Running
on
Zero
derektan
commited on
Commit
·
e595323
1
Parent(s):
eb3bad4
[BUGFIX] Forces process thread to stop if page is refreshed while operation is done
Browse files
app.py
CHANGED
|
@@ -16,6 +16,7 @@ import matplotlib
|
|
| 16 |
matplotlib.use("Agg", force=True)
|
| 17 |
|
| 18 |
import gradio as gr
|
|
|
|
| 19 |
import os, glob, threading, time
|
| 20 |
import torch
|
| 21 |
from PIL import Image
|
|
@@ -31,6 +32,21 @@ from test_multi_robot_worker import TestWorker # noqa: E402
|
|
| 31 |
from Taxabind.TaxaBind.SatBind.clip_seg_tta import ClipSegTTA # noqa: E402
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# CHANGE ME!
|
| 35 |
POLL_INTERVAL = 0.1 # For visualization
|
| 36 |
|
|
@@ -182,23 +198,29 @@ def process(
|
|
| 182 |
|
| 183 |
t = threading.Thread(target=planner.run_episode, args=(0,), daemon=True)
|
| 184 |
t.start()
|
| 185 |
-
# planner.run_episode(0)
|
| 186 |
|
| 187 |
-
sent = set()
|
| 188 |
-
last_img = None
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
sent
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
print("planner.perf_metrics: ", planner.perf_metrics)
|
| 204 |
|
|
|
|
| 16 |
matplotlib.use("Agg", force=True)
|
| 17 |
|
| 18 |
import gradio as gr
|
| 19 |
+
import ctypes # for safely stopping background threads
|
| 20 |
import os, glob, threading, time
|
| 21 |
import torch
|
| 22 |
from PIL import Image
|
|
|
|
| 32 |
from Taxabind.TaxaBind.SatBind.clip_seg_tta import ClipSegTTA # noqa: E402
|
| 33 |
|
| 34 |
|
| 35 |
+
# Helper to kill a Python thread by injecting SystemExit
|
| 36 |
+
|
| 37 |
+
def _stop_thread(thread: threading.Thread):
|
| 38 |
+
"""Forcefully raise SystemExit in the given thread (best-effort)."""
|
| 39 |
+
if thread is None or not thread.is_alive():
|
| 40 |
+
return
|
| 41 |
+
tid = thread.ident
|
| 42 |
+
if tid is None:
|
| 43 |
+
return
|
| 44 |
+
# Ask CPython to raise SystemExit in the thread context
|
| 45 |
+
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(SystemExit))
|
| 46 |
+
if res > 1:
|
| 47 |
+
# If it returned >1, cleanup and fail safe
|
| 48 |
+
ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), None)
|
| 49 |
+
|
| 50 |
# CHANGE ME!
|
| 51 |
POLL_INTERVAL = 0.1 # For visualization
|
| 52 |
|
|
|
|
| 198 |
|
| 199 |
t = threading.Thread(target=planner.run_episode, args=(0,), daemon=True)
|
| 200 |
t.start()
|
|
|
|
| 201 |
|
| 202 |
+
sent: set[str] = set()
|
| 203 |
+
last_img: str | None = None
|
| 204 |
+
try:
|
| 205 |
+
while t.is_alive():
|
| 206 |
+
# discover any new pngs written by TestWorker
|
| 207 |
+
pngs = glob.glob(os.path.join(gifs_path, "*.png"))
|
| 208 |
+
pngs.sort(key=lambda p: int(os.path.splitext(os.path.basename(p))[0]))
|
| 209 |
+
for fp in pngs:
|
| 210 |
+
if fp not in sent:
|
| 211 |
+
sent.add(fp)
|
| 212 |
+
last_img = fp
|
| 213 |
+
yield fp # stream update
|
| 214 |
+
time.sleep(POLL_INTERVAL)
|
| 215 |
+
finally:
|
| 216 |
+
# This block runs when the generator is cancelled (e.g. page refresh)
|
| 217 |
+
if t.is_alive():
|
| 218 |
+
_stop_thread(t)
|
| 219 |
+
t.join(timeout=1)
|
| 220 |
+
|
| 221 |
+
# If the episode finished naturally, send the last frame once more
|
| 222 |
+
if last_img is not None:
|
| 223 |
+
yield last_img
|
| 224 |
|
| 225 |
print("planner.perf_metrics: ", planner.perf_metrics)
|
| 226 |
|