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
Cache objects should be closed at the end of an application lifecycle, with
await Cache.close(), or usingasync 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.
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.Or more succinctly: