Skip to content

grpc.aio Dependancy Injection Best Practice #6134

Answered by jgould22
jgould22 asked this question in Questions
Discussion options

You must be logged in to vote

Hi @jccarles thanks for your input. I eventually landed on something quite similar that looks a little like the below

I am not sure if it is perfect but it appears to be working

For what it is worth I also ran into the duplicate event loop issue

from fastapi import FastAPI

import grpc
import helloworld_pb2
import helloworld_pb2_grpc
from settings import get_settings

settings = get_settings()

# Create App
app = FastAPI()

# Init the grpc channel connection for rpc
# Based on https://github.com/tiangolo/fastapi/issues/4035#issuecomment-941293227
@app.on_event("startup")
async def init_channel() -> None:
    """
    Initialize the GRPC Channel on startup
    Reused a single grpc channel t…

Replies: 4 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@Gujiawei-Edinburgh
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by Kludex
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question or problem question-migrate
5 participants
Converted from issue

This discussion was converted from issue #5438 on February 27, 2023 21:24.