You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: rodi/docs/async.md
+13-11Lines changed: 13 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,16 +4,17 @@ Support for async resolution is intentionally out of the scope of the library be
4
4
constructing objects should be lightweight.
5
5
6
6
This page provides guidelines for working with objects that require asynchronous
7
-
initialization.
7
+
initialization or disposal.
8
8
9
9
## A common example
10
10
11
-
A common example of this situation are objects that handle TCP/IP connection pooling,
12
-
such as `HTTP` clients and database clients. These objects are usually implemented as
13
-
*context managers* in Python because they need to implement connection pooling and
14
-
gracefully close TCP connections when disposed.
11
+
A common example of objects requiring asynchronous disposal are objects that
12
+
handle TCP/IP connection pooling, such as `HTTP` clients and database clients.
13
+
These objects are typically implemented as *context managers* in Python because
14
+
they need to manage connection pooling and gracefully close TCP connections
15
+
upon disposal.
15
16
16
-
Python supports[`asynchronous` context managers](https://peps.python.org/pep-0492/#asynchronous-context-managers-and-async-with) for this kind of scenario.
17
+
Python provides[`asynchronous` context managers](https://peps.python.org/pep-0492/#asynchronous-context-managers-and-async-with) for this kind of scenario.
17
18
18
19
Consider the following example, of a `SendGrid` API client to send emails using the
19
20
SendGrid API, with asynchronous code and using [`httpx`](https://www.python-httpx.org/async/).
@@ -82,7 +83,7 @@ class SendGridClient(EmailHandler):
82
83
},
83
84
json=self.get_body(email),
84
85
)
85
-
#Note: in case of error, inspect response.text
86
+
#TODO: in case of error, log response.text
86
87
response.raise_for_status() # Raise an error for bad responses
87
88
88
89
defget_body(self, email: Email) -> dict:
@@ -110,7 +111,7 @@ the one shown on this page to send emails using SendGrid in async code.
110
111
///
111
112
112
113
The **SendGridClient** depends on an instance of `SendGridClientSettings` (providing a
113
-
SendGrid API Key), and on an instance of `httpx.AsyncClient`able to make HTTP requests.
114
+
SendGrid API Key), and on an instance of `httpx.AsyncClient` to make async HTTP requests.
114
115
115
116
The code below shows how to register the object that requires asynchronous
116
117
initialization and use it across the lifetime of your application.
@@ -182,9 +183,10 @@ HTTP client disposed
182
183
183
184
## Considerations
184
185
185
-
- It is not Rodi's responsibility to administer the lifecycle of the application. It is
186
-
the responsibility of the code that bootstrap the application, to handle objects that
187
-
require asynchronous initialization and disposal.
186
+
- It is not Rodi's responsibility to administer the lifecycle of the
187
+
application. It is the responsibility of the code that bootstraps the
188
+
application, to handle objects that require asynchronous initialization and
189
+
disposal.
188
190
- Python's `asynccontextmanager` is convenient for these scenarios.
189
191
- In the example above, the HTTP Client is configured as singleton to benefit from TCP
190
192
connection pooling. It would also be possible to configure it as transient or scoped
Note that, in practice, this does not cause any issues at runtime, because of
378
+
Note that, in practice, this does not cause issues at runtime, because of
379
379
**type erasure**. For more information, refer to [_Instantiating generic classes and type erasure_](https://typing.python.org/en/latest/spec/generics.html#instantiating-generic-classes-and-type-erasure).
380
380
381
381
If you need to define a more specialized class for `Repository[Product]`,
0 commit comments