mirror of
https://github.com/kennethreitz/responder.git
synced 2026-06-05 06:46:14 +00:00
43c803a426
These serve as illustrative markers for users reading the example. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
27 lines
539 B
Python
27 lines
539 B
Python
# Example showing the lifespan context manager pattern.
|
|
# https://pypi.org/project/responder/
|
|
from contextlib import asynccontextmanager
|
|
|
|
import responder
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app):
|
|
# Startup: initialize resources
|
|
print("Starting up...")
|
|
yield
|
|
# Shutdown: clean up resources
|
|
print("Shutting down...")
|
|
|
|
|
|
api = responder.API(lifespan=lifespan)
|
|
|
|
|
|
@api.route("/{greeting}")
|
|
async def greet_world(req, resp, *, greeting):
|
|
resp.text = f"{greeting}, world!"
|
|
|
|
|
|
if __name__ == "__main__":
|
|
api.run()
|