Aiosetups -

# ... your app logic here ...

def add(self, name: str, init_func, cleanup_func=None): async def _init(): self._resources[name] = await init_func() self._init_tasks.append(_init) if cleanup_func: async def _clean(): await cleanup_func(self._resources[name]) self._cleanup_tasks.insert(0, _clean) # reverse order cleanup aiosetups

await setup.cleanup_all() if == " main ": asyncio.run(main()) Better: asynccontextmanager approach @asynccontextmanager async def aiosetup(*resources): """Context manager for async setup/cleanup.""" initialized = [] try: for res in resources: inst = await res["init"]() initialized.append((res["name"], inst, res.get("cleanup"))) yield name: inst for name, inst, _ in initialized finally: for name, inst, cleanup in reversed(initialized): if cleanup: await cleanup(inst) Usage async def main(): async with aiosetup( "name": "db", "init": init_postgres, "cleanup": close_postgres, "name": "redis", "init": init_redis ) as resources: db = resources["db"] redis = resources["redis"] # work with db & redis If you meant a real PyPI package No package named aiosetups exists on PyPI (checked as of 2026). Common alternatives: Common alternatives:

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More