]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Document when to use AsyncClient (#534)
authorFlorimond Manca <florimond.manca@gmail.com>
Fri, 22 Nov 2019 08:19:39 +0000 (09:19 +0100)
committerGitHub <noreply@github.com>
Fri, 22 Nov 2019 08:19:39 +0000 (09:19 +0100)
* Document when to use AsyncClient

* Strip advice on reverting to Requests

docs/async.md

index 45620bf8c276ac9f249b7d7121f5fcdfbf6ea66c..c631d5455125de5a89d830fb7ecf7f4d18554063 100644 (file)
@@ -105,3 +105,38 @@ trio.run(main)
 
 !!! important
     `trio` must be installed to import and use the `TrioBackend`.
+
+## FAQ
+
+### When should I use an `AsyncClient`?
+
+You should use an `AsyncClient` whenever you are inside an *async environment*.
+
+In particular, using `httpx.get()` or `httpx.Client()` in an async environment is **not** supported. There are several technical reasons to this, but the rationale is that you shouldn't be doing blocking-style network calls in the context of an event loop (for more discussion, see [#179](https://github.com/encode/httpx/issues/179)).
+
+For example, this won't work:
+
+```python
+import asyncio
+import httpx
+
+async def main():
+    r = httpx.get("https://example.org")
+
+asyncio.run(main())
+```
+
+Instead, you should use an `AsyncClient`:
+
+```python
+import asyncio
+import httpx
+
+async def main():
+    async with httpx.AsyncClient() as client:
+        r = await client.get("https://example.org")
+
+asyncio.run(main())
+```
+
+If you *need* to make synchronous requests, or otherwise run into issues related to sync usage, you should probably consider using a regular threaded client such as [Requests](https://github.com/psf/requests) instead.