dependency-injector-tenant-singleton
Tenant-scoped singleton provider for the dependency-injector framework.
Overview
TenantSingleton is a provider that creates singleton instances scoped to individual tenants. Unlike a global singleton that returns the same instance everywhere, TenantSingleton returns the same instance for the same tenant while providing different instances for different tenants.
This is useful in multi-tenant applications where you need:
- Separate configurations per tenant
- Isolated resources for each tenant
- Tenant-scoped caching or state
Installation
pip install dependency-injector-tenant-singleton
Usage
from dependency_injector import containers, providers
from dependency_injector_tenant_singleton import TenantSingleton
def get_current_tenant() -> str:
"""Get the current tenant identifier."""
return "tenant-a"
class Container(containers.DeclarativeContainer):
#: Current active tenant
active_tenant = providers.Factory(get_current_tenant)
#: Tenant-scoped service instance
service = TenantSingleton(
active_tenant,
SomeService,
# Additional args/kwargs are passed to the factory
)
container = Container()
# Same tenant - same instance
with container.active_tenant.override("tenant-a"):
instance1 = container.service()
instance2 = container.service()
assert instance1 is instance2 # Same instance for tenant-a
# Different tenant - different instance
with container.active_tenant.override("tenant-b"):
instance3 = container.service()
assert instance3 is not instance1 # Different instance for tenant-b
# Back to tenant-a - same instance as before
with container.active_tenant.override("tenant-a"):
instance4 = container.service()
assert instance4 is instance1 # Same instance again
How It Works
TenantSingleton accepts a provider that returns a tenant identifier and a factory callable:
TenantSingleton(tenant_provider, factory, *args, **kwargs)
tenant_provider- A provider that returns the current tenant identifierfactory- Callable that creates the instance (class, function, etc.)*args,**kwargs- Additional arguments passed to the factory
Internally, TenantSingleton maintains a mapping of tenant identifiers to singleton instances. When called, it:
- Resolves the current tenant identifier from
tenant_provider - Creates a singleton for that tenant if it doesn't exist
- Returns the tenant's singleton instance
License
MIT License
Release files for dependency-injector-tenant-singleton 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dependency_injector_tenant_singleton-1.0.1.tar.gz | 3.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dependency_injector_tenant_singleton-1.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.2 kB
Release files / dependency_injector_tenant_singleton-1.0.1.tar.gz
| Download URL | dependency_injector_tenant_singleton-1.0.1.tar.gz |
|---|---|
| Size | 3.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d4da4eb9ef284c633f79b3ce96efc99444393c2e57a6447b3376f2f6b9817fe4
|
|
BLAKE2b-256 checksum How to use checksums |
02eaed441c0c59b21545227176aa8e07c33ac4b3925e25d407b511f02dbfc496
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / dependency_injector_tenant_singleton-1.0.1-py3-none-any.whl
| Download URL | dependency_injector_tenant_singleton-1.0.1-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ba67aec720c6102bc531f3b50cd8567392222d4cf615ae5a575041860cdcfd94
|
|
BLAKE2b-256 checksum How to use checksums |
33f43751100b5f541d86c9e1e7a66b95dd44401caf74acd1d28f6b3e56ca78cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|