-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Closed
Labels
Description
First Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google "How to X in FastAPI" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
# An example based very roughly on https://github.com/grpc/grpc/blob/master/examples/python/helloworld/async_greeter_client.py
from fastapi import FastAPI, Depends
app = FastAPI()
import grpc
import helloworld_pb2
import helloworld_pb2_grpc
async def get_stub():
async with grpc.aio.insecure_channel('localhost:50051') as channel:
stub = helloworld_pb2_grpc.GreeterStub(channel)
yield stub
@app.get("/")
async def my_endpoint_with_a_grpc_stub(stub = Depends(get_stub)):
response = await stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
return f"Greeter client received: {response.message}"Description
I am looking for advice on how to correctly use FastAPIs dependency injection the GRPC aio library (I need to use this to make requests to a dependent service)
The examples all seem to open a channel and use it but then the docs for the sync bits imply that I should multiplex over a single channel https://github.com/grpc/grpc/tree/master/examples/python/multiplex while the AIO docs warn of thread safety and other things (https://grpc.github.io/grpc/python/grpc_asyncio.html) .
Unlike the sync lib I cannot init the channel when the file is imported because there is no event loop when that occurs. Should I be doing it in a startup event ?
Has anyone used the grpc.aio lib with fast api?
Operating System
Linux
Operating System Details
Debian / Ubuntu containers
FastAPI Version
0.85.0
Python Version
3.10
Additional Context
No response