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
To make asynchronous requests, you'll need an `AsyncClient`.
16
16
17
17
```python
18
-
>>> client = httpx.AsyncClient()
19
-
>>> r =await client.get('https://www.example.com/')
18
+
>>>asyncwith httpx.AsyncClient() as client:
19
+
>>> r =await client.get('https://www.example.com/')
20
+
>>> r
21
+
<Response [200OK]>
20
22
```
21
23
24
+
!!! tip
25
+
Use [IPython](https://ipython.readthedocs.io/en/stable/) to try this code interactively, as it supports executing `async`/`await` expressions in the console.
26
+
27
+
!!! note
28
+
The `async with` syntax ensures that all active connections are closed on exit.
29
+
30
+
It is safe to access response content (e.g. `r.text`) both inside and outside the `async with` block, unless you are using response streaming. In that case, you should `.read()`, `.stream()`, or `.close()` the response *inside* the `async with` block.
31
+
22
32
## API Differences
23
33
24
34
If you're using streaming responses then there are a few bits of API that
25
35
use async methods:
26
36
27
37
```python
28
-
>>> client = httpx.AsyncClient()
29
-
>>> r =await client.get('https://www.example.com/', stream=True)
30
-
>>>try:
38
+
>>>asyncwith httpx.AsyncClient() as client:
39
+
>>> r =await client.get('https://www.example.com/', stream=True)
31
40
>>>asyncfor chunk in r.stream():
32
41
>>>...
33
-
>>>finally:
34
-
>>>await r.close()
35
42
```
36
43
37
44
The async response methods are:
@@ -41,15 +48,15 @@ The async response methods are:
41
48
*`.raw()`
42
49
*`.close()`
43
50
44
-
If you're making parallel requests, then you'll also need to use an async API:
51
+
If you're making [parallel requests](/parallel/), then you'll also need to use an async API:
0 commit comments