Skip to content

Commit

Permalink
[PR #6163/a0a9b3db backport][3.8] Add clarification to configuring app (
Browse files Browse the repository at this point in the history
#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>
  • Loading branch information
5 people committed Nov 23, 2021
1 parent f1815e6 commit b28cee8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/4137.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added clarification on configuring the app object with settings such as a db connection.
11 changes: 11 additions & 0 deletions docs/web_advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ Data Sharing aka No Singletons Please
:mod:`aiohttp.web` discourages the use of *global variables*, aka *singletons*.
Every variable should have its own context that is *not global*.

Global variables are generally considered bad practice due to the complexity
they add in keeping track of state changes to variables.

*aiohttp* does not use globals by design, which will reduce the number of bugs
and/or unexpected behaviors for its users. For example, an i18n translated string
being written for one request and then being served to another.

So, :class:`Application` and :class:`Request`
support a :class:`collections.abc.MutableMapping` interface (i.e. they are
dict-like objects), allowing them to be used as data stores.
Expand Down Expand Up @@ -272,6 +279,10 @@ For this please use :attr:`Request.config_dict` read-only property::
async def handler(request):
data = request.config_dict['my_private_key']

The app object can be used in this way to reuse a database connection or anything
else needed throughout the application.

See this reference section for more detail: :ref:`aiohttp-web-app-and-router`.

Request's storage
^^^^^^^^^^^^^^^^^
Expand Down

0 comments on commit b28cee8

Please sign in to comment.