Comments (6)
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.
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.
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.
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.
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.
Thanks!
from aiodynamo.
Related Issues (20)
- 21.12 breaks some apis HOT 2
- Maybe summarise how DynamoDB works
- FileCredentials never set aws_session_token HOT 1
- Incompatible argument type issues after version 22.2 HOT 2
- KeyConditionExpression does not allow IN
- Support for applications running on Kubernetes
- after moving to github actions, integration tests are no longer run
- Add Amazon DynamoDB Accelerator (DAX) support to the library HOT 3
- Surprising behaviour in `Client.table_exists` HOT 1
- Auth Error With No Default Credentials HOT 1
- Unable to use AWS_REGION env var HOT 3
- Support AWS Web Identity Token File authentication HOT 2
- Shall we try goodfirstissue.dev ?
- Empty-set safety HOT 3
- Get most recently added entry HOT 5
- ECS Fargate Credentials Compatibility HOT 2
- Support for typing-extensions version 4 module HOT 2
- Update maximum transaction operations to 100 HOT 1
- InstanceMetadataCredentials does not work with IMDSv2 HOT 5
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 aiodynamo.