Skip to content

Commit

Permalink
uses LockCM in all code
Browse files Browse the repository at this point in the history
  • Loading branch information
jlgerber committed Mar 30, 2019
1 parent 78a4fc1 commit 357bc30
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/service_locator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __enter__(self):
def __exit__(self, exc_type, exc_value, exc_traceback):
_release_lock()


class ServiceLocator(object):
"""
Class which tracks services. A service may be a python instance
Expand Down Expand Up @@ -109,6 +110,13 @@ def register(self, key, service):
TypeError
If the key supplied is not hashable.
"""
with LockCM() as lock:
if self.key_is_superclass:
if inspect.isclass(service):
assert issubclass(service, key)
else:
assert isinstance(service, key)
ServiceLocator._services[key] = service
# try:
# _acquire_lock()
# if self.key_is_superclass:
Expand All @@ -119,13 +127,6 @@ def register(self, key, service):
# ServiceLocator._services[key] = service
# finally:
# _release_lock()
with LockCM() as lock:
if self.key_is_superclass:
if inspect.isclass(service):
assert issubclass(service, key)
else:
assert isinstance(service, key)
ServiceLocator._services[key] = service

@classmethod
def has_service(cls, service_key):
Expand All @@ -144,13 +145,13 @@ def has_service(cls, service_key):
TypeError
If `service_key` is unhashable.
"""
with LockCM() as lock:
return cls._services.has_key(service_key)
# try:
# _acquire_lock()
# return cls._services.has_key(service_key)
# finally:
# _release_lock()
with LockCM() as lock:
return cls._services.has_key(service_key)

@classmethod
def service(cls, service_key):
Expand All @@ -174,14 +175,14 @@ def service(cls, service_key):
TypeError
If the supplied `service_key` is unhashable
"""
with LockCM() as lock:
return cls._services[service_key]
# try:
# _acquire_lock()
# return cls._services[service_key]
# finally:
# _release_lock()

with LockCM() as lock:
return cls._services[service_key]

@classmethod
def services(cls):
Expand All @@ -197,13 +198,13 @@ def services(cls):
[ Hashable,... ]
Shallow copy of the list of registered service keys.
"""
with LockCM() as lock:
return cls._services.keys()[:]
# try:
# _acquire_lock()
# return cls._services.keys()[:]
# finally:
# _release_lock()
with LockCM() as lock:
return cls._services.keys()[:]


SERVICE_LOCATOR = ServiceLocator()
Expand Down

0 comments on commit 357bc30

Please sign in to comment.