Skip to main content

IoC container for Pip.Services in Python

Project description

Pip.Services Logo
IoC container for Python

This module is a part of the Pip.Services polyglot microservices toolkit. It provides an inversion-of-control (IoC) container to facilitate the development of services and applications composed of loosely coupled components.

The module containes a basic in-memory container that can be embedded inside a service or application, or can be run by itself. The second container type can run as a system level process and can be configured via command line arguments. Also it can be used to create docker containers.

The containers can read configuration from JSON or YAML files use it as a recipe for instantiating and configuring components. Component factories are used to create components based on their locators (descriptor) defined in the container configuration. The factories shall be registered in containers or dynamically in the container configuration file.

The module contains the following packages:

  • Core - Basic in-memory and process containers
  • Build - Default container factory
  • Config - Container configuration components
  • Refer - Inter-container reference management (implementation of the Referenceable pattern inside an IoC container)

Quick links:

Use

Install the Python package as

pip install pip_services3_container

Create a factory to create components based on their locators (descriptors).

from pip_services3_commons.refer import Descriptor
from pip_services3_components.build import Factory


class MyFactory(Factory):
    MyComponentDescriptor = Descriptor("myservice", "mycomponent", "default", "*", "1.0")

    def __init__(self):
        super(MyFactory, self).__init__()
        self.register_as_type(MyFactory.MyComponentDescriptor, MyComponent)

Then create a process container and register the factory there. You can also register factories defined in other modules if you plan to include external components into your container.

from pip_services3_container import ProcessContainer
from pip_services3_rpc.build import DefaultRpcFactory


class MyProcess(ProcessContainer):
    def __init__(self):
        super(MyProcess, self).__init__('myservice', 'My service running as a process')

        self._factories.add(DefaultRpcFactory())
        self._factories.add(MyFactory())

Define YAML configuration file with components and their descriptors. The configuration file is pre-processed using Handlebars templating engine that allows to inject configuration parameters or dynamically include/exclude components using conditional blocks. The values for the templating engine are defined via process command line arguments or via environment variables. Support for environment variables works well in docker or other containers like AWS Lambda functions.

---
# Context information
- descriptor: "pip-services:context-info:default:default:1.0"
  name: myservice
  description: My service running in a process container

# Console logger
- descriptor: "pip-services:logger:console:default:1.0"
  level: {{LOG_LEVEL}}{{^LOG_LEVEL}}info{{/LOG_LEVEL}}

# Performance counters that posts values to log
- descriptor: "pip-services:counters:log:default:1.0"
  
# My component
- descriptor: "myservice:mycomponent:default:default:1.0"
  param1: XYZ
  param2: 987
  
{{#if HTTP_ENABLED}}
# HTTP endpoint version 1.0
- descriptor: "pip-services:endpoint:http:default:1.0"
  connection:
    protocol: "http"
    host: "0.0.0.0"
    port: {{HTTP_PORT}}{{^HTTP_PORT}}8080{{/HTTP_PORT}}

 # Default Status
- descriptor: "pip-services:status-service:http:default:1.0"

# Default Heartbeat
- descriptor: "pip-services:heartbeat-service:http:default:1.0"
{{/if}}

To instantiate and run the container we need a simple process launcher.

import sys
from MyFactory import MyFactory

try:
    proc = MyProcess()
    proc._config_path = './config/config.yml'
    proc.run()
except Exception as ex:
    sys.stderr.write(ex)

And, finally, you can run your service launcher as

python service.py

Develop

For development you shall install the following prerequisites:

  • Python 3.7+
  • Visual Studio Code or another IDE of your choice
  • Docker

Install dependencies:

pip install -r requirements.txt

Run automated tests:

python test.py

Generate API documentation:

./docgen.ps1

Before committing changes run dockerized build and test as:

./build.ps1
./test.ps1
./clear.ps1

Contacts

The Python version of Pip.Services is created and maintained by Sergey Seroukhov

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pip_services3_container-3.2.0.tar.gz (17.9 kB view details)

Uploaded Source

File details

Details for the file pip_services3_container-3.2.0.tar.gz.

File metadata

  • Download URL: pip_services3_container-3.2.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.2

File hashes

Hashes for pip_services3_container-3.2.0.tar.gz
Algorithm Hash digest
SHA256 d2eca976b89d28cf9e99bbcbb2c71b06974935cb47dd95ca1377e1e9ec75ea62
MD5 2fa9fc5ce957ab1730185d72082baf25
BLAKE2b-256 a758ab092e79e27a66f8214a55ac10e50929889be62e2302c2080a86510d658a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page