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
30 lines (26 loc) · 778 Bytes
/
connections.py
File metadata and controls
30 lines (26 loc) · 778 Bytes
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
from threading import Lock
import json
class Connections:
def __init__(self, ioloop):
self.lock = Lock()
self._ioloop = ioloop
self._clients = []
def add_connection(self, handler):
self.lock.acquire()
try:
self._clients.append(handler)
finally:
self.lock.release()
def close_connection(self, handler):
self.lock.acquire()
try:
self._clients.remove(handler)
finally:
self.lock.release()
def message_clients(self, message):
self.lock.acquire()
try:
for client in self._clients:
self._ioloop.add_callback(client.write_message, json.dumps(message))
finally:
self.lock.release()