Skip to content

Commit b749738

Browse files
committed
Simplify locked map
1 parent d3cab53 commit b749738

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

template/server/utils/locks.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
import asyncio
22

33

4-
class LockedMap:
4+
class LockedMap(dict):
55
def __init__(self):
6-
self.map_lock = asyncio.Lock()
7-
self.map = {}
8-
self.locks = {}
9-
10-
def get(self, key):
11-
return self.map.get(key)
12-
13-
def set(self, key, value):
14-
self.map[key] = value
6+
super().__init__()
7+
self._map_lock = asyncio.Lock()
8+
self._locks = {}
159

1610
async def get_lock(self, key):
17-
await self.map_lock.acquire()
18-
if key not in self.locks:
19-
self.locks[key] = asyncio.Lock()
11+
await self._map_lock.acquire()
12+
if key not in self._locks:
13+
self._locks[key] = asyncio.Lock()
2014

21-
lock = self.locks[key]
15+
lock = self._locks[key]
2216
print(f"Lock acquired for {key}")
23-
self.map_lock.release()
17+
self._map_lock.release()
2418
return lock
25-
26-
def __getitem__(self, key):
27-
return self.get(key)
28-
29-
def __setitem__(self, key, value):
30-
self.set(key, value)

0 commit comments

Comments
 (0)