Coder Social home page Coder Social logo

Session instantiation about aiodynamo HOT 6 CLOSED

torphix avatar torphix commented on June 21, 2024
Session instantiation

from aiodynamo.

Comments (6)

dimaqq avatar dimaqq commented on June 21, 2024

You can totally do that. I’ve done it. It’s particularly meaningful in case of FastAPI, as there were issues with starlette and async contexts.

Your code as stated may be enough if your app never exits, eg runs until container is killed.

On the other hand, if your app is instantiated many times in tests, you can add a shutdown hook too to make the lifecycle cleaner.

from aiodynamo.

ojii avatar ojii commented on June 21, 2024

Therefore my approach has been to create a session once upon the starting of my server like so:

your approach is similar to the one we use with fastapi, though we explicitly close the http session on app shutdown.

Would it be better to instantiate a new session every time I need to make queries to the database?

That would be worse. You could create aiodynamo.client.Client instances for each query if for some reason you want to do so, but the http session and (non-static) credentials instances benefit from being long lived. For http sessions it's so the http client can have a connection pool to re-use and for credentials it's to cache credentials if they're not static.

from aiodynamo.

dimaqq avatar dimaqq commented on June 21, 2024

P.S. @torphix what kind of environment does your code run in? If it's a lambda, then a different set of concerns applies.

from aiodynamo.

torphix avatar torphix commented on June 21, 2024

Thanks for getting back to me :)
The environment is indeed using AWS lambda + Mangum + AWS API gateway
What sort of alterations / design patterns changes should be adhered to?

from aiodynamo.

dimaqq avatar dimaqq commented on June 21, 2024

That has it's own set of issues, incl. jordaneremieff/mangum#208 ➡️ jordaneremieff/mangum#211 in short, watch out for your on_event("startup") actually getting called for every request.

Basically you want to prioritise fast startup over everything else, and even then possibly use the lifecycle hooks.

Perhaps a global singleton is better... but then if lambda is suspended and resumed you may get errors or slowdown if the dynamodb key has expired... ultimately you probably need to use the lifecycle hooks 🤷🏻

from aiodynamo.

torphix avatar torphix commented on June 21, 2024

Thanks!

from aiodynamo.

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.