betty.service module - Betty documentation

Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# betty.service module

An API for providing application-wide services.

class betty.service.Bootstrapped

  Bases: `object`

  A component that can be in a bootstrapped state.

  This is internal. It MAY be used anywhere in Betty’s source code, but MUST NOT be used by third-party code.

  __init__(*args: Any, **kwargs: Any)
  final assert_bootstrapped() → None

    Assert that the component has been bootstrapped.

  final assert_not_bootstrapped() → None

    Assert that the component was not bootstrapped.

  property bootstrapped: bool

    Whether the component has been bootstrapped.

exception betty.service.BootstrappedError

  Bases: `ServiceError`

  Something was unexpectedly bootstrapped already.

exception betty.service.NotBootstrappedError

  Bases: `ServiceError`

  Something was unexpectedly not yet bootstrapped.

exception betty.service.ServiceError

  Bases: `RuntimeError`

  A service API error.

  This is internal. It MAY be used anywhere in Betty’s source code, but MUST NOT be used by third-party code.

exception betty.service.ServiceInitializedError

  Bases: `ServiceError`

  A service was unexpectedly initialized already.

class betty.service.ServiceManager

  Bases: `Generic`[`_ServiceProviderT`, `_ServiceGetT`, `_ServiceT`]

  Manages a single service for a service provider.

  This is internal. It MAY be used anywhere in Betty’s source code, but MUST NOT be used by third-party code.

  __init__(factory: Callable[[_ServiceProviderT], _ServiceGetT], *, shared: bool)
  get(instance: _ServiceProviderT) → _ServiceGetT

    Get the service from an instance.

  get_state(instance: _ServiceProviderT) → dict[str, Any]

    Get the attribute’s state for the given instance.

    The returned state is the subset of `instance.__dict__` owned by this descriptor and that must be pickled along with `instance`.

  property is_shared: bool

    Whether the service is shared between service provider instances.

  override(instance: _ServiceProviderT, service: _ServiceT) → None

    Override the service for the given instance.

    Calling this will prevent any existing factory from being called.

    This MUST only be called from `instance.__init__()`.

    The provided service MUST be pickleable.

  override_factory(instance: _ServiceProviderT, factory: Callable[[_ServiceProviderT], _ServiceGetT]) → None

    Override the default service factory for the given instance.

    This MUST only be called from `instance.__init__()`. It will override the existing service factory method defined on the instance.

    The provided factory MUST be pickleable.

class betty.service.ServiceProvider

  Bases: `Bootstrapped`, `Shutdownable`

  A service provider.

  Service providers make up a running Betty ‘application’. They can provide services through `betty.service.service()`, and manage their resources by being bootstrapped and shut down.

  Service providers may be pickled once bootstrapped. Unpickled service providers are bootstrapped, and must be shut down by the caller.

  __init__(*args: Any, **kwargs: Any)
  async bootstrap() → None

    Bootstrap the component.

  async shutdown(*, wait: bool = True) → None

    Shut the component down.

class betty.service.ShutdownCallbackKwargs

  Bases: `TypedDict`

  The keyword arguments to a shutdown callback.

  wait: bool

    `True` to wait for the component to shut down gracefully, or `False` to attempt an immediate forced shutdown.

final class betty.service.ShutdownStack

  Bases: `Bootstrapped`, `Shutdownable`

  A stack that invokes callbacks in reverse order upon shutting down.

  __init__()
  append(callback: Callable[[Unpack[ShutdownCallbackKwargs]], Awaitable[None]] | Shutdownable) → None

    Append a callback or another component to the stack.

  async shutdown(*, wait: bool = True) → None

    Shut the component down.

class betty.service.Shutdownable

  Bases: `ABC`

  A component that can be shut down.

  abstractmethod async shutdown(*, wait: bool = True) → None

    Shut the component down.

class betty.service.StaticService

  Bases: `Generic`[`_ServiceProviderT`, `_ServiceT`]

  A service factory that returns a static, predefined service.

  This can be pickled.

  This is thread-safe, which means you can safely use this between different threads.

  This is process-safe, which means you can safely use this between different processes.

  This is internal. It MAY be used anywhere in Betty’s source code, but MUST NOT be used by third-party code.

  __init__(service: _ServiceT)

betty.service.service(factory: Callable[[_ServiceProviderT], Awaitable[_ServiceT]], shared: bool = False) → _AsynchronousServiceManager[_ServiceProviderT, _ServiceT]
betty.service.service(factory: Callable[[_ServiceProviderT], _ServiceT], shared: bool = False) → _SynchronousServiceManager[_ServiceProviderT, _ServiceT]
betty.service.service(factory: None = None, shared: bool = False) → _ServiceDecorator

  Decorate a service factory method.

  The factory method is replaced with a `service manager` which handles lazy service instantiation, caching, and multiprocessing support.

  The decorated factory method should return a new service instance.