Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,7 +9,44 @@ import pandas as pd
|
|
| 9 |
from datetime import datetime
|
| 10 |
from typing import Dict, Any, List, Optional
|
| 11 |
import threading
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# ----------------------------------------------------------------------
|
| 15 |
# Plotly for dashboards
|
|
@@ -362,23 +399,8 @@ def refresh_dashboard():
|
|
| 362 |
)
|
| 363 |
|
| 364 |
# ----------------------------------------------------------------------
|
| 365 |
-
#
|
| 366 |
# ----------------------------------------------------------------------
|
| 367 |
-
def log_memory_usage():
|
| 368 |
-
"""Periodically log memory usage to help diagnose timeouts."""
|
| 369 |
-
try:
|
| 370 |
-
process = psutil.Process(os.getpid())
|
| 371 |
-
mem_info = process.memory_info()
|
| 372 |
-
logger.info(f"Memory usage: RSS={mem_info.rss / 1e6:.1f} MB, VMS={mem_info.vms / 1e6:.1f} MB")
|
| 373 |
-
# Also log system memory if available
|
| 374 |
-
sys_mem = psutil.virtual_memory()
|
| 375 |
-
logger.info(f"System memory: {sys_mem.percent}% used, {sys_mem.available / 1e9:.1f} GB free")
|
| 376 |
-
except Exception as e:
|
| 377 |
-
logger.error(f"Failed to log memory: {e}")
|
| 378 |
-
# Schedule next check in 60 seconds
|
| 379 |
-
threading.Timer(60, log_memory_usage).start()
|
| 380 |
-
|
| 381 |
-
# Start memory logging (once)
|
| 382 |
log_memory_usage()
|
| 383 |
|
| 384 |
# ----------------------------------------------------------------------
|
|
|
|
| 9 |
from datetime import datetime
|
| 10 |
from typing import Dict, Any, List, Optional
|
| 11 |
import threading
|
| 12 |
+
|
| 13 |
+
# ----------------------------------------------------------------------
|
| 14 |
+
# Memory monitoring (no external dependencies)
|
| 15 |
+
# ----------------------------------------------------------------------
|
| 16 |
+
def get_memory_usage():
|
| 17 |
+
"""Return current process memory usage in MB (RSS)."""
|
| 18 |
+
try:
|
| 19 |
+
# Try using resource module (Unix-like)
|
| 20 |
+
import resource
|
| 21 |
+
rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
|
| 22 |
+
# On Linux, ru_maxrss is in kilobytes; on macOS, in bytes
|
| 23 |
+
if rss < 1e9: # likely kilobytes
|
| 24 |
+
return rss / 1024.0 # convert to MB
|
| 25 |
+
else:
|
| 26 |
+
return rss / (1024.0 * 1024.0) # convert to MB
|
| 27 |
+
except ImportError:
|
| 28 |
+
# Fallback to reading /proc/self/status (Linux)
|
| 29 |
+
try:
|
| 30 |
+
with open("/proc/self/status") as f:
|
| 31 |
+
for line in f:
|
| 32 |
+
if line.startswith("VmRSS:"):
|
| 33 |
+
parts = line.split()
|
| 34 |
+
if len(parts) >= 2:
|
| 35 |
+
# Value in kB
|
| 36 |
+
return int(parts[1]) / 1024.0 # convert to MB
|
| 37 |
+
except Exception:
|
| 38 |
+
pass
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
def log_memory_usage():
|
| 42 |
+
"""Periodically log memory usage to help diagnose timeouts."""
|
| 43 |
+
mem_mb = get_memory_usage()
|
| 44 |
+
if mem_mb is not None:
|
| 45 |
+
logging.info(f"Process memory: {mem_mb:.1f} MB")
|
| 46 |
+
else:
|
| 47 |
+
logging.info("Process memory: unknown")
|
| 48 |
+
# Schedule next check in 60 seconds
|
| 49 |
+
threading.Timer(60, log_memory_usage).start()
|
| 50 |
|
| 51 |
# ----------------------------------------------------------------------
|
| 52 |
# Plotly for dashboards
|
|
|
|
| 399 |
)
|
| 400 |
|
| 401 |
# ----------------------------------------------------------------------
|
| 402 |
+
# Start memory monitoring (non‑blocking)
|
| 403 |
# ----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
log_memory_usage()
|
| 405 |
|
| 406 |
# ----------------------------------------------------------------------
|