pip_services3_container package

Module contents


Contains implementation of the inversion of control container, which creates objects and controls their lifecycle(*) using various configurations.

Using generic containers, we can create more specialized containers – one of which is the process container. It represents a system process, receives its configuration file via the command line, and creates a container, starts it, reads its configuration, recreates objects, runs them, and then, after pressing ctrl-c, turns off and destroys the objects.

Another example of containers are lambda functions, service fabric containers, and so on.

### (*) Compont lifecycle: ###

External configurations (stored as YAML or JSON) are passed to the container and define the structure of objects that need to be recreated in the container. Objects can be defined in two ways:

  • using descriptors (using which registered factories can recreate the object)

  • using hard-coded types (objects are recreated directly, based on their type, bypassing factories).

In addition, various configurations are stored for each object. The container recreates the objects and, if they implement the IConfigurable interface, passes them their configurations. Once the objects of a container are configured, if they implement the IReferencable interface, they are passed a set of references for recreating links between objects in the container. If objects implement the IOpenable interface, the open() method is called and they start to work. Connections to various services are made, after which the objects start, the container starts running, and the objects carry out their tasks. When the container starts to close, the objects that implement the ICloseable interface are closed via their close() method (which should make them stop working and disconnect from other services), after which objects that implement the IUnreferencable interface delete various links between objects, and, finally, the contains destroys all objects and turns off.


Conceptual Vision Consulting LLC 2018-2019, see AUTHORS for more details.


MIT, see LICENSE for more details.

class pip_services3_container.Container(name: str = None, description: str = None)

Bases: pip_services3_commons.config.IConfigurable.IConfigurable, pip_services3_commons.refer.IReferenceable.IReferenceable, pip_services3_commons.refer.IUnreferenceable.IUnreferenceable, pip_services3_commons.run.IOpenable.IOpenable

Inversion of control (IoC) container that creates components and manages their lifecycle.

The container is driven by configuration, that usually stored in JSON or YAML file. The configuration contains a list of components identified by type or locator, followed by component configuration.

On container start it performs the following actions:
  • Creates components using their types or calls registered factories to create components using their locators

  • Configures components that implement IConfigurable interface and passes them their configuration parameters

  • Sets references to components that implement IReferenceable interface and passes them references of all components in the container

  • Opens components that implement IOpenable interface

On container stop actions are performed in reversed order:
  • Closes components that implement ICloseable interface

  • Unsets references in components that implement IUnreferenceable interface

  • Destroys components in the container.

The component configuration can be parameterized by dynamic values. That allows specialized containers to inject parameters from command line or from environment variables.

The container automatically creates a ContextInfo component that carries detail information about the container and makes it available for other components.

### Configuration parameters ###
  • name: the context (container or process) name

  • description: human-readable description of the context

  • properties: entire section of additional descriptive properties


======= config.yaml ========
- descriptor: mygroup:mycomponent1:default:default:1.0
param1: 123
param2: ABC

- type: mycomponent2,mypackage
param1: 321
param2: XYZ
container = Container()

parameters = ConfigParams.from_value(os.env)
container.read_config_from_file("123", "./config/config.yml", parameters)

print "Container is opened"
# process...
print "Container is closed"
add_factory(factory: pip_services3_components.build.IFactory.IFactory)

Adds a factory to the container. The factory is used to create components added to the container by their locators (descriptors).


factory – a component factory to be added.

close(correlation_id: Optional[str])

Closes component and frees used resources.


correlation_id – (optional) transaction id to trace execution through call chain.

configure(config: pip_services3_commons.config.ConfigParams.ConfigParams)

Configures component by passing configuration parameters.


config – configuration parameters to be set.


Checks if the component is opened.


true if the component has been opened and false otherwise.

open(correlation_id: Optional[str])

Opens the component.


correlation_id – (optional) transaction id to trace execution through call chain.

read_config_from_file(correlation_id: Optional[str], path: str, parameters: pip_services3_commons.config.ConfigParams.ConfigParams)

Reads container configuration from JSON or YAML file and parameterizes it with given values.

  • correlation_id – (optional) transaction id to trace execution through call chain.

  • path – a path to configuration file

  • parameters – values to parameters the configuration or null to skip parameterization.

set_references(references: pip_services3_commons.refer.IReferences.IReferences)

Sets references to dependent components.


references – references to locate the component dependencies.


Unsets (clears) previously set references to dependent components.

class pip_services3_container.ProcessContainer(name: str = None, description: str = None)

Bases: pip_services3_container.Container.Container

Inversion of control (IoC) container that runs as a system process. It processes command line arguments and handles unhandled exceptions and Ctrl-C signal to gracefully shutdown the container.

### Command line arguments ###
  • –config -c path to JSON or YAML file with container configuration (default: “./config/config.yml”)

  • –param –params -p value(s) to parameterize the container configuration

  • –help -h prints the container usage help


container = ProcessContainer()


Runs the container by instantiating and running components inside the container.

It reads the container configuration, creates, configures, references and opens components. On process exit it closes, unreferences and destroys components to gracefully shutdown.