Spaces:
Sleeping
Sleeping
zhimin-z
commited on
Commit
·
bc2c415
1
Parent(s):
25857b6
refine
Browse files
msr.py
CHANGED
|
@@ -284,16 +284,31 @@ def get_duckdb_connection():
|
|
| 284 |
"""
|
| 285 |
Initialize DuckDB connection with OPTIMIZED memory settings.
|
| 286 |
Uses persistent database and reduced memory footprint.
|
|
|
|
| 287 |
"""
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
|
| 290 |
# OPTIMIZED SETTINGS
|
| 291 |
conn.execute(f"SET threads TO {DUCKDB_THREADS};")
|
| 292 |
conn.execute("SET preserve_insertion_order = false;")
|
| 293 |
conn.execute("SET enable_object_cache = true;")
|
| 294 |
conn.execute("SET temp_directory = '/tmp/duckdb_temp';")
|
| 295 |
-
conn.execute(f"SET memory_limit = '{DUCKDB_MEMORY_LIMIT}';")
|
| 296 |
-
conn.execute(f"SET max_memory = '{DUCKDB_MEMORY_LIMIT}';")
|
| 297 |
|
| 298 |
return conn
|
| 299 |
|
|
|
|
| 284 |
"""
|
| 285 |
Initialize DuckDB connection with OPTIMIZED memory settings.
|
| 286 |
Uses persistent database and reduced memory footprint.
|
| 287 |
+
Automatically removes cache file if lock conflict is detected.
|
| 288 |
"""
|
| 289 |
+
try:
|
| 290 |
+
conn = duckdb.connect(DUCKDB_CACHE_FILE)
|
| 291 |
+
except Exception as e:
|
| 292 |
+
# Check if it's a locking error
|
| 293 |
+
error_msg = str(e)
|
| 294 |
+
if "lock" in error_msg.lower() or "conflicting" in error_msg.lower():
|
| 295 |
+
print(f" ⚠ Lock conflict detected, removing {DUCKDB_CACHE_FILE}...")
|
| 296 |
+
if os.path.exists(DUCKDB_CACHE_FILE):
|
| 297 |
+
os.remove(DUCKDB_CACHE_FILE)
|
| 298 |
+
print(f" ✓ Cache file removed, retrying connection...")
|
| 299 |
+
# Retry connection after removing cache
|
| 300 |
+
conn = duckdb.connect(DUCKDB_CACHE_FILE)
|
| 301 |
+
else:
|
| 302 |
+
# Re-raise if it's not a locking error
|
| 303 |
+
raise
|
| 304 |
|
| 305 |
# OPTIMIZED SETTINGS
|
| 306 |
conn.execute(f"SET threads TO {DUCKDB_THREADS};")
|
| 307 |
conn.execute("SET preserve_insertion_order = false;")
|
| 308 |
conn.execute("SET enable_object_cache = true;")
|
| 309 |
conn.execute("SET temp_directory = '/tmp/duckdb_temp';")
|
| 310 |
+
conn.execute(f"SET memory_limit = '{DUCKDB_MEMORY_LIMIT}';") # Per-query limit
|
| 311 |
+
conn.execute(f"SET max_memory = '{DUCKDB_MEMORY_LIMIT}';") # Hard cap
|
| 312 |
|
| 313 |
return conn
|
| 314 |
|