async.md
This file
Path
docs/async.md
59 lines · 1.6 KiB · Markdown
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960# Async Client
HTTPX offers a standard synchronous API by default, but also gives youthe option of an async client if you need it.
Async is a concurrency model that is far more efficient than multi-threading,and can provide significant performance benefits and enable the use oflong-lived network connections such as WebSockets.
If you're working with an async web framework then you'll also want to use anasync client for sending outgoing HTTP requests.
## Making Async requests
To make asynchronous requests, you'll need an `AsyncClient`.
```python>>> client = httpx.AsyncClient()>>> r = await client.get('https://www.example.com/')```
## API Differences
If you're using streaming responses then there are a few bits of API thatuse async methods:
```python>>> client = httpx.AsyncClient()>>> r = await client.get('https://www.example.com/', stream=True)>>> try:>>> async for chunk in r.stream():>>> ...>>> finally:>>> await r.close()```
The async response methods are:
* `.read()`* `.stream()`* `.raw()`* `.close()`
If you're making parallel requests, then you'll also need to use an async API:
```python>>> client = httpx.AsyncClient()>>> async with client.parallel() as parallel:>>> pending_one = parallel.get('https://example.com/1')>>> pending_two = parallel.get('https://example.com/2')>>> response_one = await pending_one.get_response()>>> response_two = await pending_two.get_response()```
The async parallel methods are:
* `.parallel()` *Used as an "async with" context manager.** `.get_response()`* `.next_response()`