Skip to content
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

Merged
merged 5 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -239,6 +239,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 @@ -271,6 +278,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