-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsafe_structs.py
More file actions
32 lines (28 loc) · 763 Bytes
/
Copy pathsafe_structs.py
File metadata and controls
32 lines (28 loc) · 763 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
31
32
#!/usr/bin/env python3
from threading import Lock
class SafeSet(set):
def __init__(self, *args, **kwargs):
self._lock = Lock()
super(SafeSet, self).__init__(*args, **kwargs)
def add(self, elem):
self._lock.acquire()
try:
super(SafeSet, self).add(elem)
finally:
self._lock.release()
def remove(self, elem):
self._lock.acquire()
try:
super(SafeSet, self).remove(elem)
finally:
self._lock.release()
def clone(self) -> set:
self._lock.acquire()
result = None
try:
result = self.copy()
except:
result = None
finally:
self._lock.release()
return result