Aiosetups 2021 · Essential

# ... your app logic here ...

async def close_postgres(resource): print(f"Closing resource")

await setup.setup_all() print("Resources ready:", setup._resources) aiosetups

def get(self, name: str): return self._resources.get(name) async def init_postgres(): # Imagine asyncpg.connect(...) return "conn": "fake pg connection"

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 Common alternatives: async def main(): setup = AsyncSetup()

| Package | Purpose | |----------------|--------------------------------------| | async-lru | Async caching | | aiofiles | Async file I/O | | aioredis | Redis client | | asyncpg | PostgreSQL driver | | anyio / asyncio | Low-level async primitives |

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: setup._resources) def get(self

async def main(): setup = AsyncSetup() setup.add("db", init_postgres, close_postgres) setup.add("cache", init_redis)