Replies: 1 comment 1 reply
-
To support Litestar, the approach aligns closely with the logic used for FastAPI. In order to utilize your FastStream brokers, it is necessary to provide a context manager responsible for managing your broker's startup and shutdown as an argument to the Litestar lifespan parameter. You can find more details about this in the Litestar documentation on lifespan context managers. Following this setup, you can execute your Litestar application as usual, and the broker will seamlessly start alongside it. Here is and example on how it could be done: from contextlib import asynccontextmanager
from litestar import Litestar, get
from faststream.kafka import KafkaBroker
# Define your broker and async endpoints
broker = KafkaBroker("localhost:9092")
@broker.subscriber("test")
async def base_handler(body):
print(body)
# Create a context manager for broker startup/shutdown
@asynccontextmanager
async def lifespan(app: Litestar):
await broker.start()
yield
await broker.close()
# Define your Litestar application and pass the broker context manager to its lifespan
@get("/")
def read_root():
return {"Hello": "World"}
app = Litestar([get], lifespan=[lifespan]) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
What about support for working with LiteStar? Benchmarks
Beta Was this translation helpful? Give feedback.
All reactions