Spaces:
Sleeping
Sleeping
Factor Studios
commited on
Update websocket_storage.py
Browse files- websocket_storage.py +23 -2
websocket_storage.py
CHANGED
|
@@ -6,8 +6,25 @@ import threading
|
|
| 6 |
from queue import Queue
|
| 7 |
import time
|
| 8 |
import asyncio
|
|
|
|
|
|
|
| 9 |
class WebSocketGPUStorage:
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
self.url = url
|
| 12 |
self.websocket = None
|
| 13 |
self.connected = False
|
|
@@ -20,7 +37,6 @@ class WebSocketGPUStorage:
|
|
| 20 |
self.last_error_time = 0
|
| 21 |
self.max_retries = 5
|
| 22 |
self.tensor_registry: Dict[str, Dict[str, Any]] = {} # Track tensor metadata
|
| 23 |
-
self.resource_monitor = {'vram_used': 0, 'active_tensors': 0}
|
| 24 |
self.model_registry: Dict[str, Dict[str, Any]] = {} # Track loaded models
|
| 25 |
self.resource_monitor = {
|
| 26 |
'vram_used': 0,
|
|
@@ -31,6 +47,11 @@ class WebSocketGPUStorage:
|
|
| 31 |
# Start WebSocket connection in a separate thread
|
| 32 |
self.ws_thread = threading.Thread(target=self._run_websocket_loop, daemon=True)
|
| 33 |
self.ws_thread.start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def _run_websocket_loop(self):
|
| 36 |
self._loop = asyncio.new_event_loop()
|
|
|
|
| 6 |
from queue import Queue
|
| 7 |
import time
|
| 8 |
import asyncio
|
| 9 |
+
import hashlib
|
| 10 |
+
|
| 11 |
class WebSocketGPUStorage:
|
| 12 |
+
# Singleton instance
|
| 13 |
+
_instance = None
|
| 14 |
+
_lock = threading.Lock()
|
| 15 |
+
|
| 16 |
+
def __new__(cls, url: str = "wss://factorst-wbs1.hf.space/ws"):
|
| 17 |
+
with cls._lock:
|
| 18 |
+
if cls._instance is None:
|
| 19 |
+
cls._instance = super().__new__(cls)
|
| 20 |
+
cls._instance._init_singleton(url)
|
| 21 |
+
return cls._instance
|
| 22 |
+
|
| 23 |
+
def _init_singleton(self, url: str):
|
| 24 |
+
"""Initialize the singleton instance"""
|
| 25 |
+
if hasattr(self, 'initialized'):
|
| 26 |
+
return
|
| 27 |
+
|
| 28 |
self.url = url
|
| 29 |
self.websocket = None
|
| 30 |
self.connected = False
|
|
|
|
| 37 |
self.last_error_time = 0
|
| 38 |
self.max_retries = 5
|
| 39 |
self.tensor_registry: Dict[str, Dict[str, Any]] = {} # Track tensor metadata
|
|
|
|
| 40 |
self.model_registry: Dict[str, Dict[str, Any]] = {} # Track loaded models
|
| 41 |
self.resource_monitor = {
|
| 42 |
'vram_used': 0,
|
|
|
|
| 47 |
# Start WebSocket connection in a separate thread
|
| 48 |
self.ws_thread = threading.Thread(target=self._run_websocket_loop, daemon=True)
|
| 49 |
self.ws_thread.start()
|
| 50 |
+
self.initialized = True
|
| 51 |
+
|
| 52 |
+
def __init__(self, url: str = "wss://factorst-wbs1.hf.space/ws"):
|
| 53 |
+
"""This will actually just return the singleton instance"""
|
| 54 |
+
pass
|
| 55 |
|
| 56 |
def _run_websocket_loop(self):
|
| 57 |
self._loop = asyncio.new_event_loop()
|