forked from ragadeeshu/mp3printer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnections.py
More file actions
39 lines (31 loc) · 1.07 KB
/
Copy pathconnections.py
File metadata and controls
39 lines (31 loc) · 1.07 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
# pyright: strict
import threading
import tornado.ioloop
import tornado.websocket
import _types
class Connections:
def __init__(self, ioloop: tornado.ioloop.IOLoop):
self.lock = threading.Lock()
self._ioloop = ioloop
self._clients: list[tornado.websocket.WebSocketHandler] = []
def add_connection(self, handler: tornado.websocket.WebSocketHandler):
self.lock.acquire()
try:
self._clients.append(handler)
finally:
self.lock.release()
def close_connection(self, handler: tornado.websocket.WebSocketHandler):
self.lock.acquire()
try:
self._clients.remove(handler)
finally:
self.lock.release()
def message_clients(self, message: _types.Response):
self.lock.acquire()
try:
for client in self._clients:
self._ioloop.add_callback( # pyright: ignore[reportUnknownMemberType]
client.write_message, message.model_dump_json()
)
finally:
self.lock.release()