Skip to content

Lifecycle management of decorators #609

Open
@Dreamsorcerer

Description

@Dreamsorcerer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions