Lifecycle management of decorators
- Dominant language
- Python
- Stars
- 1.4k
- Forks
- 181
- PR merge metrics
- No merged PRs in 30d
Description
Cache objects should be closed at the end of an application lifecycle, with `await Cache.close()`, or using `async with`.
The current design of decorators creates a new cache instance with each decorator and no attempt to close it is made, thus failing to manage this lifecycle management properly.
e.g.
```
@cached(...) # New cache is created here, but never closed.
def foo(): ...
```
We also want to consider using aiojobs for managing some background tasks, but this additionally requires being created within a running loop, something which is unlikely when a decorator is called.
----
One solution I can think of, is to explicitly manage the caches, and pass them to the decorators. This may also need a `.start()` method to initiate the cache later. e.g.
```
cache = Cache(...)
@cached(cache)
def foo(): ...
async def main():
# Initialise application
cache.start()
# Run application
...
# Cleanup application
await cache.close()
```
Or more succinctly:
```
async def main():
async with cache:
# Run application
```
Contributor guide
Assessment
This issue has not been assessed yet.