betty.service module¶
An API for providing application-wide services.
- class betty.service.Bootstrapped[source]¶
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.
- exception betty.service.BootstrappedError[source]¶
Bases:
ServiceError
Something was unexpectedly bootstrapped already.
- exception betty.service.NotBootstrappedError[source]¶
Bases:
ServiceError
Something was unexpectedly not yet bootstrapped.
- exception betty.service.ServiceError[source]¶
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[source]¶
Bases:
ServiceError
A service was unexpectedly initialized already.
- class betty.service.ServiceManager[source]¶
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.
- get_state(instance: _ServiceProviderT) dict[str, Any] [source]¶
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 withinstance
.
Whether the service is shared between service provider instances.
- override(instance: _ServiceProviderT, service: _ServiceT) None [source]¶
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 [source]¶
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[source]¶
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.
- class betty.service.ShutdownCallbackKwargs[source]¶
Bases:
TypedDict
The keyword arguments to a shutdown callback.
- final class betty.service.ShutdownStack[source]¶
Bases:
Bootstrapped
,Shutdownable
A stack that invokes callbacks in reverse order upon shutting down.
- append(callback: Callable[[Unpack[ShutdownCallbackKwargs]], Awaitable[None]] | Shutdownable) None [source]¶
Append a callback or another component to the stack.
- class betty.service.StaticService[source]¶
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.
- betty.service.service(factory: Callable[[_ServiceProviderT], Awaitable[_ServiceT]], shared: bool = False) _AsynchronousServiceManager[_ServiceProviderT, _ServiceT] [source]¶
- 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.