Comments (8)
Just want to drop a message here to provide some extra context and remind myself and people who may come across this in the future:
- The discussion here is based on the previous implementation of getting the database session using middleware which is different from the current method of using
yield
.
Ref:
- codebase time travel back to 2019
- Current relevant documentation on db session middleware: https://fastapi.tiangolo.com/tutorial/sql-databases/#alternative-db-session-with-middleware
from full-stack-fastapi-postgresql.
Great responses as always @dmontagu, thanks for all your help here! 👏🍰🚀
Thanks @jim-obrien-orig for reporting back and closing the issue 🎉
from full-stack-fastapi-postgresql.
Just wanted to update that I ran parallel tests on this (curls and gnu parallel). I scattered get all on endpoints with posts - pretty heavy json and writing to a wide table.
All was good.
from full-stack-fastapi-postgresql.
from full-stack-fastapi-postgresql.
I don't believe using a scoped_session would play nicely with session middleware, since the middleware will be executed in a different thread than the endpoint callable (since it will be run in the threadpool, as you noted).
I was also worried about the threadsafety issue, and I actually had a lengthy discussion about this with @tiangolo on gitter (a couple months ago, I believe), and in the end I became convinced there isn't actually any risk here:
- A new session is created for each request, and is only accessed on the main (middleware) thread and in the
run_in_threadpool
thread - the main (middleware) thread does NOT interact with the session while the threadpool call is being executed; similarly, the run_in_threadpool thread does NOT interact with the session before or after the
run_in_threadpool
call is finished - So there is no need for any sort of extra thread-safety synchronization
- (alternatively, you could think of the endpoint execution as providing the synchronization)
If anyone reads this and thinks it is wrong, please correct me!
from full-stack-fastapi-postgresql.
Ok - that's good to know. Follow up question is about the caching of depends. In this implementation, it uses Depends(get_db)
without use_cache=False
. Does that mean you could get a different Session
than the one created for the request. Or is the caching session level?
from full-stack-fastapi-postgresql.
If use_cache=True
(the default), the callable will be executed at most once per request.
However, this only applies to Depends
-based dependencies. Since the session is usually created in middleware, you just need to make sure you aren't creating a separate session in your Depends
calls.
If you create the session in middleware, and just access it in the Depends
calls, you shouldn't have any problems with multiple sessions being created.
Does that answer your question?
from full-stack-fastapi-postgresql.
@jim-obrien-orig if you find any issues in your testing, please do share!
from full-stack-fastapi-postgresql.
Related Issues (20)
- How to start the backend server without using docker?
- FASTAPI
- 🐛 Error in frontend when resetting a password: "The passwords do not match" HOT 1
- AttributeError: module 'app.db.engine' has no attribute '_run_ddl_visitor' HOT 1
- How can I override my JWT auth dependency in the endponits in FastAPI testing?
- How do I get this running on local? HOT 3
- Deploying on a machine with nginx HOT 1
- Why React, not Vue?
- Mac M1 Docker error HOT 4
- Bad Gateway returned HOT 3
- [Improvement] Replace Prettier and Eslint with Biome and add pre-commit hooks HOT 1
- Feature Request: Support for GitHub Codespaces Deployment and Development HOT 2
- Login error in Dashboard HOT 5
- Secrets should not be versioned by default HOT 3
- Enable discussion on this repo? HOT 1
- Support the use of dev containers for reproducible dev environment HOT 1
- Embracing Technological Evolution: A Journey Toward Innovation
- Debug Launch Configuration for "Debug Frontend: (...)" does not work on WSL
- Z
- Running task 1 of 1: python .copier/update_dotenv.py /bin/sh: 1: python: not found HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from full-stack-fastapi-postgresql.