-
|
With But if you don't use middleware, there is no problem with 100/sec loads at the same time. First look at the code below. This is an example code of fastapi. ...
app = FastAPI()
app.add_middleware(middleware_class=BaseHTTPMiddleware, dispatch="some_function")
...
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
...
@app.get("/users/", response_model=List[schemas.User])
async def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
users = crud.get_users(db, skip=skip, limit=limit)
return users
async def some_function(request: Request, call_next):
response = await call_next(request)
return responseWhen using middleware, if the database connection is delayed, why is the http connection affected? I am so excited to meet fastapi!! thanks. :) Environmentfastapi==0.63.0 |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
|
I believe you should use a regular function instead of a regular function while using time taking synchronous components like a db. As stated here FastAPI (Starlette) runs the async function in the current thread whereas regular functions are ran through an external thread pool.
In your case the async function is ran in the main thread, hence blocking it from doing anything else. Simply changing the async function to a regular one will let FastAPI know it could be blocking and should be ran in a different thread |
Beta Was this translation helpful? Give feedback.
-
|
@FalseDev Thanks for the reply. My problem is with the use of middleware. If I don't use middleware, there is no problem. And the code above is a sample, so I wrote it wrong. I used await for the DB connection part. e.g.) |
Beta Was this translation helpful? Give feedback.
-
|
sorry. It was a personal security tunnel problem. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks for the help here @FalseDev ! 👏 🙇 Thanks for reporting back and closing the issue @jybaek 👍
|
Beta Was this translation helpful? Give feedback.
sorry. It was a personal security tunnel problem.