Use a services registry in a Python application
This blog post shows how to use a registry to manage services in a Python application.
Introduction
This post example is based on the services-registry package, providing a registry capable of:
- initialization of services on demand. A service is initialized at most once,
- keeping track of all initialized services,
- providing services handlers on demand.
services-registry package installation
In the terminal window, execute:
pip install services-registry
Example service definition
First, we will define the example service MyService in services module. As described in the services-registry package documentation, registry managed services must inherit from the services_registry.services_registry.Service class.
from services_registry.services_registry import Service
class MyService(Service):
def __init__(self):
print(f"{__class__.__name__} initialized")
def __str__(self):
return __class__.__name__
def use(self):
print(f"Use {__class__.__name__}")
For the sake of example:
- at the
MyServiceinitialization, aMyService initializedmessage will be printed out, - the class
__str__method will return the class name, - the class has a
usemethod which prints outUse MyService.
Example service usage
To demonstrate several aspects of the services registry usage, we will create a script and a helper module with a utility function.
The helper module utility function gets the MyService handler and calls the use method:
from services_registry.services_registry import services_registry
from services import MyService
def utility_function():
my_service = services_registry.get_service(MyService)
my_service.use()
To show that the MyService is initialized only once, the script does the same thing as the utility function (gets the service handler and calls the usemethod) plus calls the utility function.
from services_registry.services_registry import services_registry
from services import MyService
from helper_module import utility_function
def main():
my_service = services_registry.get_service(MyService)
my_service.use()
utility_function()
if __name__ == '__main__':
main()
After executing the script, we receive the following output:
The <class 'services.MyService'> service has not been registered yet. Initializing it
MyService initialized
MyService has been added to registry
Use MyService
Use MyService
From the output we can see that:
- the script's
my_service = services_registry.get_service(MyService)line execution resulted in theMyServiceservice initialization and adding to the registry, - the script's
my_service.use()line execution printedUse MyServiceas expected, - the utility function's
my_service = services_registry.get_service(MyService)line execution did not create a newMyServiceinstance as this service has been initialized in the script already. The service handler has just been returned from the registry, - the utility function's
my_service.use()line execution printedUse MyServiceas expected.
In case you have a question or a comment concerning this post, please send them to: [email protected].
