Oslo Service

4.5.1 · active · verified Thu Apr 16

oslo.service is a Python library that provides a robust framework for defining and running long-running services within the OpenStack ecosystem. It encapsulates patterns for service management, including handling concurrency, periodic operations, WSGI applications, and integration with systemd. The library is actively maintained as part of the OpenStack Oslo project, with version 4.5.1 being the latest as of February 2026, and receives regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a basic service using `oslo_service.service.ServiceBase` and launch it with `oslo_service.service.launch`. It includes integration with `oslo_config` for defining and reading configuration options, such as the number of worker processes. The `start`, `stop`, and `wait` methods illustrate the service lifecycle.

import os
from oslo_config import cfg
from oslo_service import service

# Define a simple configuration option
service_opts = [
    cfg.IntOpt('workers', default=1, min=1, help='Number of worker processes')
]
CONF = cfg.CONF
CONF.register_opts(service_opts, group='my_service')

class MyService(service.ServiceBase):
    def __init__(self, conf):
        super().__init__(conf)
        self.conf = conf
        self.should_stop = False
        print(f"Service initialized with {self.conf.my_service.workers} workers.")

    def start(self):
        print(f"Service worker {os.getpid()} starting...")
        # Simulate some work
        # In a real service, this would be a long-running loop or event listener

    def stop(self):
        self.should_stop = True
        print(f"Service worker {os.getpid()} stopping...")

    def wait(self):
        # In a real service, this would block until stop() is called
        import time
        while not self.should_stop:
            time.sleep(0.1)

def main():
    CONF(args=[], project='my_app') # Pass empty args to avoid argparse errors if not parsing CLI
    
    # Instantiate your service
    my_service_instance = MyService(CONF)

    # Launch the service with workers
    # Note: oslo.service's launch function creates a Launcher internally
    # and manages worker processes if `workers` > 1.
    # For a simple example, we often directly call the Service's methods.
    # For multi-process, `service.launch` is key.
    launcher = service.launch(CONF, my_service_instance, workers=CONF.my_service.workers)
    print(f"Launcher started for PID {os.getpid()} with {CONF.my_service.workers} workers.")

    # Wait for the service to complete (e.g., via signal handler or internal logic)
    launcher.wait()
    print("All services stopped.")

if __name__ == '__main__':
    # To run this, you might need to install oslo.config and oslo.service
    # and ideally run with python -m your_script_name
    # This example demonstrates the basic structure, but running actual
    # multi-worker services requires more robust signal handling and process management
    # which oslo.service provides internally.
    main()

view raw JSON →