-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Multiple (nested) StreamRouters are not initialized #1842
Comments
Did you checked our dicumentation? https://faststream.airt.ai/latest/getting-started/integrations/fastapi/#multiple-routers It's been a long time since this discussion. FastAPI and FastStream had some breaking changes Now I reccomend to use our regular FastStream routers as nested for FastAPI one - it is the most predictable behavior |
Yes, I have tried that doc as well. Am I supposed to provide connection details to all RabbitRouters? or just the "core"? Do they still share a connection? |
If you are using our Router (not FastAPI-compatible ones) the core Router copies all publishers and subscribers to itself and nested Routers doesn't require a real connection |
Anyway, can you show me reproducible example I can copy-past? |
This is an example of what is not working. It seems as though the nested ( # consolidated ...
from fastapi import FastAPI, APIRouter
from faststream.rabbit.fastapi import RabbitRouter
# ./a/router.py
a_router = RabbitRouter()
@a_router.post("/foo")
async def foo():
await a_router.broker.publish("foo")
# ./b/router.py
b_router = RabbitRouter()
# ./api.py
api_router = APIRouter()
api_router.include_router(a_router)
api_router.include_router(b_router)
# ./main.py
def create_app() -> FastAPI:
app = FastAPI()
app.include_router(api_router)
return app
app = create_app() Note, if I include # ./main.py
def create_app() -> FastAPI:
app = FastAPI()
app.include_router(a_router)
app.include_router(b_router)
return app
app = create_app() It's not what I expected, but it's not a deal breaker if that's how it works. |
@andrewduckett please, use This is the recommended in the documentation way. But, I'll try to investigate your problem and fix it as well. |
Describe the bug
Hey @Lancetnik I'm having trouble when using multiple nested StreamRouters in FastAPI. I tried following the example here with no luck #677 (reply in thread)_
The application starts just fine but when accessing the broker to publish a message I get an error that the I need to connect first.
In the discussion is sounds like a single broker connection will be shared between all of the stream routers, which is what I would like to happen. I assume this has something to do with the lifecycle of each StreamRouter.
And/Or steps to reproduce the behavior:
core_router
core_router
in FastAPIrouter.broker.publish(...)
Expected behavior
I would like to create multiple RabbitRouters, nested in an APIRouter, included in a FastAPI Application that share a connection.
Observed behavior
Screenshots
If applicable, attach screenshots to help illustrate the problem.
Environment
FastAPI version
0.115.0
The text was updated successfully, but these errors were encountered: