Skip to content

Commit

Permalink
added LockPool
Browse files Browse the repository at this point in the history
  • Loading branch information
tmaeno committed Apr 24, 2023
1 parent 204a3cc commit b57f6a8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion PandaPkgInfo.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
release_version = "0.0.33"
release_version = "0.0.34"
3 changes: 3 additions & 0 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Includes all libraries used by both server and monitor (and others).
Release Note
------------

* 0.0.34 (24/4/2023)
* added LockPool

* 0.0.32 (17/11/2022)
* heartbeat in stomp connection

Expand Down
39 changes: 39 additions & 0 deletions pandacommon/pandautils/thread_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,42 @@ def pop(self):
self.weights.put(weights)
self.data.put(data)
return d


# lock pool
class LockPool(object):

def __init__(self, pool_size=100):
self.pool_size = pool_size
self.lock = multiprocessing.Lock()
self.manager = multiprocessing.Manager()
self.key_to_lock = self.manager.dict()
self.lock_ref_count = self.manager.dict()
self.lock_pool = {i: multiprocessing.Lock() for i in range(pool_size)}

def get(self, key):
with self.lock:
if key not in self.key_to_lock:
in_used = set(self.key_to_lock.values())
free_locks = set(range(self.pool_size)).difference(in_used)
if not free_locks:
return None
index = free_locks.pop()
self.key_to_lock[key] = index
self.lock_ref_count[index] = 1
else:
index = self.key_to_lock[key]
self.lock_ref_count[index] += 1
return self.lock_pool[index]

def release(self, key):
with self.lock:
if key not in self.key_to_lock:
return
index = self.key_to_lock[key]
count = self.lock_ref_count[index]
count -= 1
if count <= 0:
count = 0
del self.key_to_lock[key]
self.lock_ref_count[index] = count

0 comments on commit b57f6a8

Please sign in to comment.