What are the possible consequences of reusing the same session throughout the app? #2719
-
We use httpx.AsyncSession for each internal host. I.e. service "dogs" makes requests to service "cats". Hence service "dogs" defines a single session for communications with service "cats". We open the session at the beginning of a fastapi process and never close it. The entire thing happens within kubernetes. Would our approach with sessions result in any unwanted behavior? I failed to find any docs or other resources that would indicate that we would encounter any issues. Note that I understand that httpx recommends always closing the session and now I'm just trying to understand the reasons behind this ;) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Before anything else, it's important to understand that each connection requires file descriptors that are kept in the process memory. |
Beta Was this translation helpful? Give feedback.
-
Great! Use a single client all the way through the lifetime of your process is how you want it. You'll get proper connection pooling throughout that way.
I'm not sure how much it matters, you'd need to really dig into the differences between how the outstanding TCP connections are closed depending on when you do explicitly close the connection pool at the end of it's lifespan vs. when you just quit the Python process. |
Beta Was this translation helpful? Give feedback.
Great! Use a single client all the way through the lifetime of your process is how you want it. You'll get proper connection pooling throughout that way.
I'm not sure how much it matters, you'd need to really dig into the differences between how the outstanding TCP connections are closed depending on when you do explicitly close the connection pool at the end of it's lifespan vs. when you just quit the Python process.