Coder Social home page Coder Social logo

Comments (10)

dmontagu avatar dmontagu commented on May 16, 2024 2

we are hoping this means that fastapi will create a new database session
for every fastapi-session/request .
I hope this is correct ?

Yes that's right.

I see two options.


Option 1: Move the config to a self-contained file that doesn't have fastapi as a dependency. Then the session.py module could be imported safely. This might not be desirable if the only setting you need in the workers is the sqlalchemy uri though.


Option 2: Make a get_sessionmaker_instance function that takes a URI as an argument:

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

# from app.core import config  # no longer necessary

def get_sessionmaker_instance(uri: str) -> sessionmaker:
    engine = create_engine(uri, pool_pre_ping=True)
    db_session = scoped_session(
        sessionmaker(autocommit=False, autoflush=False, bind=engine)
    )
    Session = sessionmaker(autocommit=False, autoflush=False, bind=engine)
    return Session

Then, instead of importing Session from this module, you import the function, and optionally create a Session in an app-specific file via
Session = get_sessionmaker_instance(config.SQLALCHEMY_DATABASE_URI), and do the same in worker code. Note -- to avoid reducing to option 1, you'll need a way to get the SQLALCHEMY_DATABASE_URI that doesn't depend on the config file. But that's going to be unavoidable if you are explicitly trying to not share dependencies that you want to use while generating your config.

from full-stack-fastapi-template.

sandys avatar sandys commented on May 16, 2024 2

closed with thanks

from full-stack-fastapi-template.

 avatar commented on May 16, 2024

Hi sandys,

You can import Session from app.db.session in your worker.py file and use it inside your tasks.

Cheers

from full-stack-fastapi-template.

sandys avatar sandys commented on May 16, 2024

from full-stack-fastapi-template.

dmontagu avatar dmontagu commented on May 16, 2024

Just put the session creation function in a self contained module that doesn't import fastapi code.

Then import that function into your fastapi code, and into your celery code.

from full-stack-fastapi-template.

sandys avatar sandys commented on May 16, 2024

from full-stack-fastapi-template.

sandys avatar sandys commented on May 16, 2024

from full-stack-fastapi-template.

dmontagu avatar dmontagu commented on May 16, 2024

@sandys What is the starlette vs pydantic config bug? I'm curious

from full-stack-fastapi-template.

sandys avatar sandys commented on May 16, 2024

from full-stack-fastapi-template.

tiangolo avatar tiangolo commented on May 16, 2024

Thanks for all the help here @dmontagu ! 🙇 🍰

@sandys I see you posted a link to another post asking about configs, here are the new FastAPI docs for configs and settings: https://fastapi.tiangolo.com/advanced/settings/

Thanks for reporting back and closing the issue 👍

from full-stack-fastapi-template.

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.