Spaces:
Sleeping
Sleeping
Factor Studios
commited on
Update test_ai_integration_http.py
Browse files- test_ai_integration_http.py +29 -33
test_ai_integration_http.py
CHANGED
|
@@ -32,25 +32,21 @@ def http_storage_manager(max_retries=5, retry_delay=2, timeout=30.0):
|
|
| 32 |
nonlocal storage
|
| 33 |
if storage:
|
| 34 |
try:
|
| 35 |
-
if storage.is_connected():
|
| 36 |
return True
|
| 37 |
storage.close()
|
| 38 |
except:
|
| 39 |
pass
|
| 40 |
-
storage = HTTPGPUStorage(
|
| 41 |
try:
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
'max_size': 10,
|
| 51 |
-
'max_retries': 3
|
| 52 |
-
}
|
| 53 |
-
})
|
| 54 |
return storage.connect()
|
| 55 |
except Exception as e:
|
| 56 |
logging.error(f"Connection configuration error: {e}")
|
|
@@ -61,8 +57,10 @@ def http_storage_manager(max_retries=5, retry_delay=2, timeout=30.0):
|
|
| 61 |
try:
|
| 62 |
if try_connect():
|
| 63 |
logging.info("Successfully connected to GPU storage server via HTTP")
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
logging.warning(f"HTTP connection attempt {attempt + 1} failed, retrying in {retry_delay}s...")
|
| 68 |
time.sleep(retry_delay * (1.5 ** attempt)) # Exponential backoff
|
|
@@ -97,28 +95,26 @@ def http_storage_manager(max_retries=5, retry_delay=2, timeout=30.0):
|
|
| 97 |
|
| 98 |
# Enhanced cleanup handler with connection management
|
| 99 |
def cleanup_resources():
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
# Clear VRAM and other resources
|
| 113 |
import gc
|
| 114 |
gc.collect()
|
| 115 |
|
| 116 |
-
try:
|
| 117 |
-
# Force close any remaining connections
|
| 118 |
-
HTTPGPUStorage.close_all_connections()
|
| 119 |
-
except Exception as e:
|
| 120 |
-
logging.error(f"Error in final connection cleanup: {e}")
|
| 121 |
-
|
| 122 |
# Register enhanced cleanup handler
|
| 123 |
atexit.register(cleanup_resources)
|
| 124 |
|
|
|
|
| 32 |
nonlocal storage
|
| 33 |
if storage:
|
| 34 |
try:
|
| 35 |
+
if hasattr(storage, 'is_connected') and storage.is_connected():
|
| 36 |
return True
|
| 37 |
storage.close()
|
| 38 |
except:
|
| 39 |
pass
|
| 40 |
+
storage = HTTPGPUStorage() # Create storage instance
|
| 41 |
try:
|
| 42 |
+
# Set connection parameters after creation
|
| 43 |
+
storage.set_timeout(timeout)
|
| 44 |
+
storage.set_max_retries(max_retries)
|
| 45 |
+
storage.set_retry_delay(retry_delay)
|
| 46 |
+
|
| 47 |
+
# Enable persistent connection if available
|
| 48 |
+
if hasattr(storage, 'enable_keep_alive'):
|
| 49 |
+
storage.enable_keep_alive()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
return storage.connect()
|
| 51 |
except Exception as e:
|
| 52 |
logging.error(f"Connection configuration error: {e}")
|
|
|
|
| 57 |
try:
|
| 58 |
if try_connect():
|
| 59 |
logging.info("Successfully connected to GPU storage server via HTTP")
|
| 60 |
+
# Simple connection test by checking server status
|
| 61 |
+
if storage.check_server_status():
|
| 62 |
+
break
|
| 63 |
+
logging.warning("Connection established but server not responding")
|
| 64 |
else:
|
| 65 |
logging.warning(f"HTTP connection attempt {attempt + 1} failed, retrying in {retry_delay}s...")
|
| 66 |
time.sleep(retry_delay * (1.5 ** attempt)) # Exponential backoff
|
|
|
|
| 95 |
|
| 96 |
# Enhanced cleanup handler with connection management
|
| 97 |
def cleanup_resources():
|
| 98 |
+
try:
|
| 99 |
+
# Get the current storage instance if it exists
|
| 100 |
+
from http_storage import HTTPGPUStorage
|
| 101 |
+
current_storage = HTTPGPUStorage.get_current_instance()
|
| 102 |
+
if current_storage is not None:
|
| 103 |
+
try:
|
| 104 |
+
# Ensure all pending operations are completed
|
| 105 |
+
if hasattr(current_storage, 'sync'):
|
| 106 |
+
current_storage.sync()
|
| 107 |
+
# Close the connection
|
| 108 |
+
current_storage.close()
|
| 109 |
+
except Exception as e:
|
| 110 |
+
logging.error(f"Error closing HTTP storage: {e}")
|
| 111 |
+
except Exception as e:
|
| 112 |
+
logging.error(f"Error in storage cleanup: {e}")
|
| 113 |
|
| 114 |
# Clear VRAM and other resources
|
| 115 |
import gc
|
| 116 |
gc.collect()
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
# Register enhanced cleanup handler
|
| 119 |
atexit.register(cleanup_resources)
|
| 120 |
|