-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathshared.py
62 lines (51 loc) · 1.11 KB
/
shared.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
## fufluns - Copyright 2019-2021 - deroad
import threading
class Shared(object):
"""Shared object"""
def __init__(self, data):
super(Shared, self).__init__()
self.lock = threading.Lock()
self.data = data
def get(self):
self.lock.acquire()
data = self.data
self.lock.release()
return data
def set(self, data):
self.lock.acquire()
self.data = data
self.lock.release()
class SharedMap(object):
"""SharedMap object"""
def __init__(self, data={}):
super(SharedMap, self).__init__()
self.lock = threading.Lock()
self.data = data
def keys(self):
self.lock.acquire()
ret = list(self.data.keys())
self.lock.release()
return ret
def has(self, key):
self.lock.acquire()
ret = key in self.data
self.lock.release()
return ret
def get(self, key):
self.lock.acquire()
data = None
if key in self.data:
data = self.data[key]
self.lock.release()
return data
def set(self, key, data):
self.lock.acquire()
self.data[key] = data
self.lock.release()
def rem(self, key):
self.lock.acquire()
try:
del self.data[key]
except KeyError:
pass
self.lock.release()