-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_file_manager.py
More file actions
65 lines (48 loc) · 1.58 KB
/
temp_file_manager.py
File metadata and controls
65 lines (48 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Centralized temporary file management with automatic cleanup on shutdown.
"""
from __future__ import annotations
import os
import logging
import atexit
import threading
from typing import Optional
log = logging.getLogger("utils.temp_file_manager")
# Global registry of all temporary files across all tools
_temp_files: list[str] = []
_cleanup_registered = False
_lock = threading.Lock()
def register_temp_file(path: Optional[str]) -> Optional[str]:
"""
Register a temporary file for cleanup on shutdown.
Thread-safe for multi-user Gradio deployments.
Args:
path: Path to temporary file
Returns:
The same path (pass-through for convenience)
"""
global _cleanup_registered
if not path:
return path
with _lock:
if path not in _temp_files:
_temp_files.append(path)
# Register cleanup on first use
if not _cleanup_registered:
atexit.register(cleanup_temp_files)
_cleanup_registered = True
return path
def cleanup_temp_files() -> None:
"""Clean up all registered temporary files. Thread-safe."""
with _lock:
if not _temp_files:
return
log.info("Cleaning up %d temporary file(s)", len(_temp_files))
for path in _temp_files:
try:
if os.path.exists(path):
os.remove(path)
log.debug("Cleaned up temporary file: %s", path)
except Exception as e:
log.warning("Failed to clean up %s: %s", path, e)
_temp_files.clear()