-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathsingleton.py
More file actions
223 lines (177 loc) · 7.93 KB
/
Copy pathsingleton.py
File metadata and controls
223 lines (177 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""Resource providers."""
import asyncio
import threading
import typing
from typing_extensions import override
from that_depends.providers.base import AbstractProvider
from that_depends.providers.mixin import ProviderWithArguments, SupportsTeardown
T_co = typing.TypeVar("T_co", covariant=True)
P = typing.ParamSpec("P")
class Singleton(ProviderWithArguments, SupportsTeardown, AbstractProvider[T_co]):
"""A provider that creates an instance once and caches it for subsequent injections.
This provider is safe to use concurrently in both threading and asyncio contexts.
On the first call to either ``resolve()`` or ``resolve_sync()``, the instance
is created by calling the provided factory. All future calls return the cached instance.
Example:
```python
def my_factory() -> float:
return 0.5
singleton = Singleton(my_factory)
value1 = singleton.resolve_sync()
value2 = singleton.resolve_sync()
assert value1 == value2
```
"""
__slots__ = "_args", "_asyncio_lock", "_factory", "_instance", "_kwargs", "_override", "_threading_lock"
def __init__(self, factory: typing.Callable[P, T_co], *args: P.args, **kwargs: P.kwargs) -> None:
"""Initialize the Singleton provider.
Args:
factory: A callable that produces the instance to be provided.
*args: Positional arguments to pass to the factory.
**kwargs: Keyword arguments to pass to the factory.
"""
super().__init__()
self._factory: typing.Final = factory
self._instance: T_co | None = None
self._asyncio_lock: typing.Final = asyncio.Lock()
self._threading_lock: typing.Final = threading.Lock()
self._args: typing.Final = args
self._kwargs: typing.Final = kwargs
def _register_arguments(self) -> None:
self._register(self._args)
self._register(self._kwargs.values())
def _deregister_arguments(self) -> None:
self._deregister(self._args)
self._deregister(self._kwargs.values())
@override
async def resolve(self) -> T_co:
if self._override is not None:
self._register_arguments()
return typing.cast(T_co, self._override)
# lock to prevent resolving several times
async with self._asyncio_lock:
if self._instance is not None:
return self._instance
self._register_arguments()
self._instance = self._factory(
*[await x.resolve() if isinstance(x, AbstractProvider) else x for x in self._args], # type: ignore[arg-type]
**{ # type: ignore[arg-type]
k: await v.resolve() if isinstance(v, AbstractProvider) else v for k, v in self._kwargs.items()
},
)
return self._instance
@override
def resolve_sync(self) -> T_co:
if self._override is not None:
self._register_arguments()
return typing.cast(T_co, self._override)
# lock to prevent resolving several times
with self._threading_lock:
if self._instance is not None:
return self._instance
self._register_arguments()
self._instance = self._factory(
*[x.resolve_sync() if isinstance(x, AbstractProvider) else x for x in self._args], # type: ignore[arg-type]
**{k: v.resolve_sync() if isinstance(v, AbstractProvider) else v for k, v in self._kwargs.items()}, # type: ignore[arg-type]
)
return self._instance
@override
async def tear_down(self, propagate: bool = True) -> None:
"""Reset the cached instance.
After calling this method, the next resolve() call will recreate the instance.
"""
if self._instance is not None:
self._instance = None
self._deregister_arguments()
if propagate:
await self._tear_down_children()
@override
def tear_down_sync(self, propagate: bool = True, raise_on_async: bool = True) -> None:
"""Reset the cached instance.
After calling this method, the next resolve call will recreate the instance.
"""
if self._instance is not None:
self._instance = None
self._deregister_arguments()
if propagate:
self._tear_down_children_sync(propagate=propagate, raise_on_async=raise_on_async)
class AsyncSingleton(ProviderWithArguments, SupportsTeardown, AbstractProvider[T_co]):
"""A provider that creates an instance asynchronously and caches it for subsequent injections.
This provider is safe to use concurrently in asyncio contexts. On the first call
to ``resolve()``, the instance is created by awaiting the provided factory.
All subsequent calls return the cached instance.
Example:
```python
async def my_async_factory() -> float:
return 0.5
async_singleton = AsyncSingleton(my_async_factory)
value1 = await async_singleton.resolve()
value2 = await async_singleton.resolve()
assert value1 == value2
```
"""
__slots__ = "_args", "_asyncio_lock", "_factory", "_instance", "_kwargs", "_override"
def __init__(
self,
factory: typing.Callable[P, typing.Awaitable[T_co]],
*args: P.args,
**kwargs: P.kwargs,
) -> None:
"""Initialize the AsyncSingleton provider.
Args:
factory: The asynchronous callable used to create the instance.
*args: Positional arguments to pass to the factory.
**kwargs: Keyword arguments to pass to the factory.
"""
super().__init__()
self._factory: typing.Final[typing.Callable[P, typing.Awaitable[T_co]]] = factory
self._instance: T_co | None = None
self._asyncio_lock: typing.Final = asyncio.Lock()
self._args: typing.Final = args
self._kwargs: typing.Final = kwargs
def _register_arguments(self) -> None:
self._register(self._args)
self._register(self._kwargs.values())
def _deregister_arguments(self) -> None:
self._deregister(self._args)
self._deregister(self._kwargs.values())
@override
async def resolve(self) -> T_co:
if self._override is not None:
return typing.cast(T_co, self._override)
# lock to prevent resolving several times
async with self._asyncio_lock:
if self._instance is not None:
return self._instance
self._register_arguments()
self._instance = await self._factory(
*[await x.resolve() if isinstance(x, AbstractProvider) else x for x in self._args], # type: ignore[arg-type]
**{ # type: ignore[arg-type]
k: await v.resolve() if isinstance(v, AbstractProvider) else v for k, v in self._kwargs.items()
},
)
return self._instance
@override
def resolve_sync(self) -> typing.NoReturn:
msg = "AsyncSingleton cannot be resolved in an sync context."
raise RuntimeError(msg)
@override
async def tear_down(self, propagate: bool = True) -> None:
"""Reset the cached instance.
After calling this method, the next call to ``resolve()`` will recreate the instance.
"""
if self._instance is not None:
self._instance = None
self._deregister_arguments()
if propagate:
await self._tear_down_children()
@override
def tear_down_sync(self, propagate: bool = True, raise_on_async: bool = True) -> None:
"""Reset the cached instance.
After calling this method, the next call to ``resolve_sync()`` will recreate the instance.
"""
if self._instance is not None:
self._instance = None
self._deregister_arguments()
if propagate:
self._tear_down_children_sync(propagate=propagate, raise_on_async=raise_on_async)