Skip to content
Myr

Improve backend docs, particularly wrt. autodetection (#595)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Improve backend docs, particularly wrt. autodetection * Resolve typo.

authored by Tom Christie · committed by GitHub · parent 1c9167e · Browse files · 1 file+38 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+38 added−10 removed

  • other:+38 added−10 removed

1 file+38 added−10 removed

modified:docs/advanced.md+38 added-10 removed
402 unmodified lines
403>>> r404Response <200 OK>405```406407## Support async environments407## Supported async environments408409### [asyncio](https://docs.python.org/3/library/asyncio.html) (Default)409HTTPX supports either `asyncio` or `trio` as an async environment.410411By default, `Client` uses `asyncio` to perform asynchronous operations and I/O calls.411By default it will auto-detect which of those two to use as the backend412for socket operations and concurrency primitives.413413### [trio](https://github.com/python-trio/trio)414You can also explicitly select a backend by instantiating a client with the415`backend` argument...416415To make asynchronous requests in `trio` programs, pass a `TrioBackend` to the `Client`:417```python418client = httpx.Client(backend='auto') # Autodetection. The default case.419client = httpx.Client(backend='asyncio') # Use asyncio as the backend.420client = httpx.Client(backend='trio') # Use trio as the backend.421```422423### [AsyncIO](https://docs.python.org/3/library/asyncio.html)424425AsyncIO is Python's [built-in library](https://docs.python.org/3/library/asyncio.html)426for writing concurrent code with the async/await syntax.427428```python418import trio429import asyncio430import httpx420from httpx.concurrency.trio import TrioBackend431432async def main():423 async with httpx.Client(backend=TrioBackend()) as client:424 ...433 client = httpx.Client()434 response = await client.get('https://www.example.com/')435 print(response)436437asyncio.run(main())438```439440### [Trio](https://github.com/python-trio/trio)441442Trio is [an alternative async library](https://trio.readthedocs.io/en/stable/),443designed around the [the principles of structured concurrency](https://en.wikipedia.org/wiki/Structured_concurrency).444445```python446import httpx447import trio448449async def main():450 client = httpx.Client()451 response = await client.get('https://www.example.com/')452 print(response)453454trio.run(main)455```456457!!! important430 `trio` must be installed to import and use the `TrioBackend`.458 The `trio` package must be installed to use the Trio backend.