Spaces:
Sleeping
Sleeping
Factor Studios
commited on
Update websocket_storage.py
Browse files- websocket_storage.py +15 -1
websocket_storage.py
CHANGED
|
@@ -7,6 +7,20 @@ from queue import Queue
|
|
| 7 |
import time
|
| 8 |
import asyncio
|
| 9 |
import hashlib
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
class WebSocketGPUStorage:
|
| 12 |
# Singleton instance
|
|
@@ -72,7 +86,7 @@ class WebSocketGPUStorage:
|
|
| 72 |
try:
|
| 73 |
while not self.message_queue.empty():
|
| 74 |
msg_id, operation = self.message_queue.get()
|
| 75 |
-
await websocket.send(json.dumps(operation))
|
| 76 |
|
| 77 |
# Wait for response with timeout
|
| 78 |
try:
|
|
|
|
| 7 |
import time
|
| 8 |
import asyncio
|
| 9 |
import hashlib
|
| 10 |
+
import dataclasses
|
| 11 |
+
|
| 12 |
+
def custom_json_serializer(obj):
|
| 13 |
+
if hasattr(obj, '__dict__'):
|
| 14 |
+
return obj.__dict__
|
| 15 |
+
elif hasattr(obj, '_asdict'): # For namedtuples
|
| 16 |
+
return obj._asdict()
|
| 17 |
+
elif dataclasses.is_dataclass(obj):
|
| 18 |
+
return dataclasses.asdict(obj)
|
| 19 |
+
elif isinstance(obj, (np.ndarray, np.number)):
|
| 20 |
+
return obj.tolist()
|
| 21 |
+
elif isinstance(obj, set):
|
| 22 |
+
return list(obj)
|
| 23 |
+
raise TypeError(f'Object of type {type(obj)} is not JSON serializable')
|
| 24 |
|
| 25 |
class WebSocketGPUStorage:
|
| 26 |
# Singleton instance
|
|
|
|
| 86 |
try:
|
| 87 |
while not self.message_queue.empty():
|
| 88 |
msg_id, operation = self.message_queue.get()
|
| 89 |
+
await websocket.send(json.dumps(operation, default=custom_json_serializer))
|
| 90 |
|
| 91 |
# Wait for response with timeout
|
| 92 |
try:
|