New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add clarification to configuring app #6163
Conversation
I don't believe this solves that issue. The issue asks for clarification and explanation about why, not how. |
Would you agree that by saving the reference to the database connection in https://docs.aiohttp.org/en/stable/web_reference.html#application Is that mainly why one should do this or are there other sections of code that look in Thanks in advance, |
Now i don't think so that is the best solution for share connection with storage or something like that. Current pattern produce a lot of drilling arguments between functions async def get_statistic(db, redis):
data = await get_statistic(db)
await inc_stats_view(redis)
return data
async def index(req):
await get_data(req.app['db'], req.app['redis])
... it's not okey. in my opinion the best way is to create a context variable for db and redis connections and then use them in I think so, problem #4137 in 2021 is not relevant, but I could be wrong |
If you compare to other projects like flask, they often use global variables ( So, I feel that the benefit is making sure everything is explicit, thus reducing the chance of bugs, by having the request/app object passed directly into the handler function and never using globals. The app object just gives us a convenient place to store these objects, but the main point is that everything is passed to the function explicitly. It is also worth noting that we encourage the same approach with handling the app object itself. In many other frameworks the app object is created as a global variable in their examples. In all the aiohttp examples, the app object is always created as a local variable in a function ( |
@Dreamsorcerer the perfect summary of my (never written maybe) thoughts when I designed this part of |
Maybe that was poor word choice calling the app object global. So the why to this issues is simply convenience? |
No, reread my comment, I was not responding to you calling it a global, but explaining the why. We should be explaining why we use the app object in this way, as opposed to the way other popular frameworks do it (i.e. avoiding globals etc.). If convenience was the only factor, we would just be lazy and use globals like flask. The app object is a convenient location for storing these things given the design parameters of aiohttp. But, it's those design parameters which are the reason we end up using it. So, any documentation for this should probably mention globals and explicitness, and how this reduces the chances of bugs in code. |
Codecov Report
@@ Coverage Diff @@
## master #6163 +/- ##
==========================================
- Coverage 93.31% 93.13% -0.19%
==========================================
Files 102 103 +1
Lines 30212 30368 +156
Branches 2708 2730 +22
==========================================
+ Hits 28192 28282 +90
- Misses 1843 1896 +53
- Partials 177 190 +13
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
0614541
to
6f83f07
Compare
…s a db connection
6f83f07
to
918c8f2
Compare
@Dreamsorcerer Thanks for explaining your thoughts @asvetlov I've amended the commit and it should cover what we've discussed here. |
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
@Arfey WDYT? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks OK to me.
Edit for clarity Co-authored-by: Sam Bull <aa6bs0@sambull.org>
Backport to 3.8: 💚 backport PR created✅ Backport PR branch: Backported as #6341 🤖 @patchback |
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com> Co-authored-by: Sam Bull <aa6bs0@sambull.org> Co-authored-by: Sviatoslav Sydorenko <webknjaz@redhat.com> (cherry picked from commit a0a9b3d)
Backport to 3.9: 💚 backport PR created✅ Backport PR branch: Backported as #6342 🤖 @patchback |
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com> Co-authored-by: Sam Bull <aa6bs0@sambull.org> Co-authored-by: Sviatoslav Sydorenko <webknjaz@redhat.com> (cherry picked from commit a0a9b3d)
#6341) Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com> Co-authored-by: Sam Bull <aa6bs0@sambull.org> Co-authored-by: Sviatoslav Sydorenko <webknjaz@redhat.com> Co-authored-by: sha016 <92833633+sha016@users.noreply.github.com>
Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com> Co-authored-by: Sam Bull <aa6bs0@sambull.org> Co-authored-by: Sviatoslav Sydorenko <webknjaz@redhat.com> (cherry picked from commit a0a9b3d) Co-authored-by: sha016 <92833633+sha016@users.noreply.github.com> Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
What do these changes do?
Added clarification on configuring the app object with setings such as a db connection
Are there changes in behavior for the user?
No
Related issue number
Closes #4137
Checklist
CONTRIBUTORS.txt
CHANGES
folder<issue_id>.<type>
for example (588.bugfix)issue_id
change it to the pr id after creating the pr.feature
: Signifying a new feature..bugfix
: Signifying a bug fix..doc
: Signifying a documentation improvement..removal
: Signifying a deprecation or removal of public API..misc
: A ticket has been closed, but it is not of interest to users.