Use a services registry in a Python application

Posted November 9, 2021 by Yaroslav Grebnov ‐ 2 min read

Requirements

This blog post shows how to use a registry to manage services in a Python application. The 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 initalized services,
  • providing services handlers on demand.

services-registry package installation

In the terminal window, execute:

pip install services-registry

Example service definition

First of all, 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 MyService initialization, a MyService initialized message will be printed out,
  • the class __str__ method will return the class name,
  • the class has a use method which prints out Use MyService.

Example service usage

In order 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()

In order 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 the MyService service initialization and adding to the registry,
  • the script’s my_service.use() line execution printed Use MyService as expected,
  • the utility function’s my_service = services_registry.get_service(MyService) line execution did not create a new MyService instance 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 printed Use MyService as expected.

In case you have a question or a comment concerning this post, please send them to: