async.md
This file
Path
docs/async.md
184 lines · 5.6 KiB · Markdown
Last changed by
Remove the UDS section from docs (#1009)Alexander Pushkovbefore adoption8ed6904
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185# Async Support
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>>> async with httpx.AsyncClient() as client:>>> r = await client.get('https://www.example.com/')>>> r<Response [200 OK]>```
!!! tip Use [IPython](https://ipython.readthedocs.io/en/stable/) or Python 3.8+ with `python -m asyncio` to try this code interactively, as they support executing `async`/`await` expressions in the console.
## API Differences
If you're using an async client then there are a few bits of API thatuse async methods.
### Making requests
The request methods are all async, so you should use `response = await client.get(...)` style for all of the following:
* `AsyncClient.get(url, ...)`* `AsyncClient.options(url, ...)`* `AsyncClient.head(url, ...)`* `AsyncClient.post(url, ...)`* `AsyncClient.put(url, ...)`* `AsyncClient.patch(url, ...)`* `AsyncClient.delete(url, ...)`* `AsyncClient.request(method, url, ...)`* `AsyncClient.send(request, ...)`
### Opening and closing clients
Use `async with httpx.AsyncClient()` if you want a context-managed client...
```pythonasync with httpx.AsyncClient() as client: ...```
Alternatively, use `await client.aclose()` if you want to close a client explicitly:
```pythonclient = httpx.AsyncClient()...await client.aclose()```
### Streaming responses
The `AsyncClient.stream(method, url, ...)` method is an async context block.
```python>>> client = httpx.AsyncClient()>>> async with client.stream('GET', 'https://www.example.com/') as response:>>> async for chunk in response.aiter_bytes():>>> ...```
The async response streaming methods are:
* `Response.aread()` - For conditionally reading a response inside a stream block.* `Response.aiter_bytes()` - For streaming the response content as bytes.* `Response.aiter_text()` - For streaming the response content as text.* `Response.aiter_lines()` - For streaming the response content as lines of text.* `Response.aiter_raw()` - For streaming the raw response bytes, without applying content decoding.* `Response.aclose()` - For closing the response. You don't usually need this, since `.stream` block close the response automatically on exit.
### Streaming requests
When sending a streaming request body with an `AsyncClient` instance, you should use an async bytes generator instead of a bytes generator:
```pythonasync def upload_bytes(): ... # yield byte content
await client.post(url, data=upload_bytes())```
## Supported async environments
HTTPX supports either `asyncio` or `trio` as an async environment.
It will auto-detect which of those two to use as the backendfor socket operations and concurrency primitives.
### [AsyncIO](https://docs.python.org/3/library/asyncio.html)
AsyncIO is Python's [built-in library](https://docs.python.org/3/library/asyncio.html)for writing concurrent code with the async/await syntax.
```pythonimport asyncioimport httpx
async def main(): async with httpx.AsyncClient() as client: response = await client.get('https://www.example.com/') print(response)
asyncio.run(main())```
### [Trio](https://github.com/python-trio/trio)
Trio is [an alternative async library](https://trio.readthedocs.io/en/stable/),designed around the [the principles of structured concurrency](https://en.wikipedia.org/wiki/Structured_concurrency).
```pythonimport httpximport trio
async def main(): async with httpx.AsyncClient() as client: response = await client.get('https://www.example.com/') print(response)
trio.run(main)```
!!! important The `trio` package must be installed to use the Trio backend.
## Calling into Python Web Apps
Just as `httpx.Client` allows you to call directly into WSGI web applications,the `httpx.AsyncClient` class allows you to call directly into ASGI web applications.
Let's take this Starlette application as an example:
```pythonfrom starlette.applications import Starlettefrom starlette.responses import HTMLResponsefrom starlette.routing import Route
async def hello(): return HTMLResponse("Hello World!")
app = Starlette(routes=[Route("/", hello)])```
We can make requests directly against the application, like so:
```python>>> import httpx>>> async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:... r = await client.get("/")... assert r.status_code == 200... assert r.text == "Hello World!"```
For some more complex cases you might need to customise the ASGI transport. This allows you to:
* Inspect 500 error responses rather than raise exceptions by setting `raise_app_exceptions=False`.* Mount the ASGI application at a subpath by setting `root_path`.* Use a given client address for requests by setting `client`.
For example:
```python# Instantiate a client that makes ASGI requests with a client IP of "1.2.3.4",# on port 123.transport = httpx.ASGITransport(app=app, client=("1.2.3.4", 123))async with httpx.AsyncClient(transport=transport, base_url="http://testserver") as client: ...```
See [the ASGI documentation](https://asgi.readthedocs.io/en/latest/specs/www.html#connection-scope) for more details on the `client` and `root_path` keys.