@@ -77,8 +77,7 @@ def is_available(self) -> bool:
7777
7878 def get (self , kind : VersionedDataKind , key : str , callback : Callable [[Any ], Any ] = lambda x : x ) -> Any :
7979 """ """
80- try :
81- self ._lock .rlock ()
80+ with self ._lock .read_lock ():
8281 itemsOfKind = self ._items [kind ]
8382 item = itemsOfKind .get (key )
8483 if item is None :
@@ -88,17 +87,12 @@ def get(self, kind: VersionedDataKind, key: str, callback: Callable[[Any], Any]
8887 log .debug ("Attempted to get deleted key %s in '%s', returning None" , key , kind .namespace )
8988 return callback (None )
9089 return callback (item )
91- finally :
92- self ._lock .runlock ()
9390
9491 def all (self , kind , callback ):
9592 """ """
96- try :
97- self ._lock .rlock ()
93+ with self ._lock .read_lock ():
9894 itemsOfKind = self ._items [kind ]
9995 return callback (dict ((k , i ) for k , i in itemsOfKind .items () if ('deleted' not in i ) or not i ['deleted' ]))
100- finally :
101- self ._lock .runlock ()
10296
10397 def init (self , all_data ):
10498 """ """
@@ -108,51 +102,39 @@ def init(self, all_data):
108102 for key , item in items .items ():
109103 items_decoded [key ] = kind .decode (item )
110104 all_decoded [kind ] = items_decoded
111- try :
112- self ._lock .rlock ()
105+ with self ._lock .write_lock ():
113106 self ._items .clear ()
114107 self ._items .update (all_decoded )
115108 self ._initialized = True
116109 for k in all_data :
117110 log .debug ("Initialized '%s' store with %d items" , k .namespace , len (all_data [k ]))
118- finally :
119- self ._lock .runlock ()
120111
121112 # noinspection PyShadowingNames
122113 def delete (self , kind , key : str , version : int ):
123114 """ """
124- try :
125- self ._lock .rlock ()
115+ with self ._lock .write_lock ():
126116 itemsOfKind = self ._items [kind ]
127117 i = itemsOfKind .get (key )
128118 if i is None or i ['version' ] < version :
129119 i = {'deleted' : True , 'version' : version }
130120 itemsOfKind [key ] = i
131- finally :
132- self ._lock .runlock ()
133121
134122 def upsert (self , kind , item ):
135123 """ """
136124 decoded_item = kind .decode (item )
137125 key = item ['key' ]
138- try :
139- self ._lock .rlock ()
126+ with self ._lock .write_lock ():
140127 itemsOfKind = self ._items [kind ]
141128 i = itemsOfKind .get (key )
142129 if i is None or i ['version' ] < item ['version' ]:
143130 itemsOfKind [key ] = decoded_item
144131 log .debug ("Updated %s in '%s' to version %d" , key , kind .namespace , item ['version' ])
145- finally :
146- self ._lock .runlock ()
147132
148133 @property
149134 def initialized (self ) -> bool :
150135 """ """
151- try :
152- self ._lock .rlock ()
136+ with self ._lock .read_lock ():
153137 return self ._initialized
154- finally :
155- self ._lock .runlock ()
156138
157139 def describe_configuration (self , config ):
158140 return 'memory'
0 commit comments