Coder Social home page Coder Social logo

Comments (8)

xinbinhuang avatar xinbinhuang commented on May 5, 2024 5

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:

  1. codebase time travel back to 2019

https://github.com/tiangolo/full-stack-fastapi-postgresql/blob/44d8a4358bc3f8c608bfad2d546aaeca074d2990/%7B%7Bcookiecutter.project_slug%7D%7D/backend/app/app/main.py#L31-L36

  1. Current relevant documentation on db session middleware: https://fastapi.tiangolo.com/tutorial/sql-databases/#alternative-db-session-with-middleware

from full-stack-fastapi-postgresql.

tiangolo avatar tiangolo commented on May 5, 2024 2

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.

jim-obrien-orig avatar jim-obrien-orig commented on May 5, 2024 2

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.

jim-obrien-orig avatar jim-obrien-orig commented on May 5, 2024 1

from full-stack-fastapi-postgresql.

dmontagu avatar dmontagu commented on May 5, 2024

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.

jim-obrien-orig avatar jim-obrien-orig commented on May 5, 2024

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.

dmontagu avatar dmontagu commented on May 5, 2024

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.

dmontagu avatar dmontagu commented on May 5, 2024

@jim-obrien-orig if you find any issues in your testing, please do share!

from full-stack-fastapi-postgresql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.