Spaces:
Running
on
Zero
Running
on
Zero
derektan
commited on
Commit
Β·
a336540
1
Parent(s):
e5beb79
Created a buffer to store N folders based on timestamp. Each run is only tagged to 1 folder, to handle concurrent users on hF
Browse files
app.py
CHANGED
|
@@ -67,6 +67,22 @@ def _kill_running_threads():
|
|
| 67 |
# Clear list regardless of alive status
|
| 68 |
_running_threads.clear()
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
# CHANGE ME!
|
| 72 |
POLL_INTERVAL = 1.0 # For visualization
|
|
@@ -223,14 +239,20 @@ def process_search_tta(
|
|
| 223 |
planner.gifs_path = save_dir
|
| 224 |
return planner
|
| 225 |
|
| 226 |
-
#
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
-
#
|
| 231 |
-
|
| 232 |
-
shutil.rmtree(_dir, ignore_errors=True)
|
| 233 |
-
os.makedirs(_dir, exist_ok=True)
|
| 234 |
|
| 235 |
# Shared dict to record if a thread hit an exception
|
| 236 |
error_flags = {"tta": False, "no": False}
|
|
|
|
| 67 |
# Clear list regardless of alive status
|
| 68 |
_running_threads.clear()
|
| 69 |
|
| 70 |
+
# ββββββββββββ Run directory rotation βββββββββββββ
|
| 71 |
+
RUN_HISTORY_LIMIT = 20 # keep at most this many timestamped run directories per instance
|
| 72 |
+
|
| 73 |
+
def _prune_old_run_dirs(base_dir: str, limit: int = RUN_HISTORY_LIMIT):
|
| 74 |
+
"""Delete oldest timestamp-named run directories leaving only *limit* of the newest ones."""
|
| 75 |
+
try:
|
| 76 |
+
dirs = [d for d in os.listdir(base_dir) if os.path.isdir(os.path.join(base_dir, d))]
|
| 77 |
+
# Timestamp format YYYYmmdd_HHMMSS ensures lexicographic order == chronological order
|
| 78 |
+
dirs.sort()
|
| 79 |
+
if len(dirs) > limit:
|
| 80 |
+
for obsolete in dirs[:-limit]:
|
| 81 |
+
shutil.rmtree(os.path.join(base_dir, obsolete), ignore_errors=True)
|
| 82 |
+
except Exception:
|
| 83 |
+
# Best-effort; ignore cleanup errors
|
| 84 |
+
pass
|
| 85 |
+
|
| 86 |
|
| 87 |
# CHANGE ME!
|
| 88 |
POLL_INTERVAL = 1.0 # For visualization
|
|
|
|
| 239 |
planner.gifs_path = save_dir
|
| 240 |
return planner
|
| 241 |
|
| 242 |
+
# ββββββββββββββ Per-run output directories ββββββββββββββ
|
| 243 |
+
# Ensure base directory exists
|
| 244 |
+
os.makedirs(gifs_path, exist_ok=True)
|
| 245 |
+
|
| 246 |
+
run_id = time.strftime("%Y%m%d_%H%M%S") # unique timestamp
|
| 247 |
+
run_root = os.path.join(gifs_path, run_id)
|
| 248 |
+
gifs_dir_tta = os.path.join(run_root, "with_tta")
|
| 249 |
+
gifs_dir_no = os.path.join(run_root, "no_tta")
|
| 250 |
+
|
| 251 |
+
os.makedirs(gifs_dir_tta, exist_ok=True)
|
| 252 |
+
os.makedirs(gifs_dir_no, exist_ok=True)
|
| 253 |
|
| 254 |
+
# House-keep old runs so we never keep more than RUN_HISTORY_LIMIT
|
| 255 |
+
_prune_old_run_dirs(gifs_path, RUN_HISTORY_LIMIT)
|
|
|
|
|
|
|
| 256 |
|
| 257 |
# Shared dict to record if a thread hit an exception
|
| 258 |
error_flags = {"tta": False, "no": False}
|