]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
📝 Add docs for using Trio with Hypercorn (#4014)
authorSebastián Ramírez <tiangolo@gmail.com>
Thu, 7 Oct 2021 10:36:09 +0000 (12:36 +0200)
committerGitHub <noreply@github.com>
Thu, 7 Oct 2021 10:36:09 +0000 (10:36 +0000)
docs/en/docs/async.md
docs/en/docs/deployment/manually.md

index 7fadee87f98550677598dc34986eb94a2d829ab4..8194650fd9748db247a952d81c7ce2a1056edc36 100644 (file)
@@ -324,7 +324,15 @@ So, about the egg and the chicken, how do you call the first `async` function?
 
 If you are working with **FastAPI** you don't have to worry about that, because that "first" function will be your *path operation function*, and FastAPI will know how to do the right thing.
 
-But if you want to use `async` / `await` without FastAPI, <a href="https://docs.python.org/3/library/asyncio-task.html#coroutine" class="external-link" target="_blank">check the official Python docs</a>.
+But if you want to use `async` / `await` without FastAPI, you can do it as well.
+
+### Write your own async code
+
+Starlette (and **FastAPI**) are based on <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a>, which makes it compatible with both Python's standard library <a href="https://docs.python.org/3/library/asyncio-task.html" class="external-link" target="_blank">asyncio</a> and <a href="https://trio.readthedocs.io/en/stable/" class="external-link" target="_blank">Trio</a>.
+
+In particular, you can directly use <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a> for your advanced concurrency use cases that require more advanced patterns in your own code.
+
+And even if you were not using FastAPI, you could also write your own async applications with <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a> to be highly compatible and get its benefits (e.g. *structured concurrency*).
 
 ### Other forms of asynchronous code
 
index 80a7df7e6a539461261df7d65038788b569d438e..6a3619b65a19619e15afe4e7dcbbffcdafcb77a9 100644 (file)
@@ -92,6 +92,43 @@ You can then your application the same way you have done in the tutorials, but w
     
     It helps a lot during **development**, but you **shouldn't** use it in **production**.
 
+## Hypercorn with Trio
+
+Starlette and **FastAPI** are based on <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a>, which makes them compatible with both Python's standard library <a href="https://docs.python.org/3/library/asyncio-task.html" class="external-link" target="_blank">asyncio</a> and <a href="https://trio.readthedocs.io/en/stable/" class="external-link" target="_blank">Trio</a>.
+
+Nevertheless, Uvicorn is currently only compatible with asyncio, and it normally uses <a href="https://github.com/MagicStack/uvloop" class="external-link" target="_blank">`uvloop`</a>, the high-performance drop-in replacement for `asyncio`.
+
+But if you want to directly use **Trio**, then you can use **Hypercorn** as it supports it. ✨
+
+### Install Hypercorn with Trio
+
+First you need to install Hypercorn with Trio support:
+
+<div class="termy">
+
+```console
+$ pip install "hypercorn[trio]"
+---> 100%
+```
+
+</div>
+
+### Run with Trio
+
+Then you can pass the command line option `--worker-class` with the value `trio`:
+
+<div class="termy">
+
+```console
+$ hypercorn main:app --worker-class trio
+```
+
+</div>
+
+And that will start Hypercorn with your app using Trio as the backend.
+
+Now you can use Trio internally in your app. Or even better, you can use AnyIO, to keep your code compatible with both Trio and asyncio. 🎉
+
 ## Deployment Concepts
 
 These examples run the server program (e.g Uvicorn), starting **a single process**, listening on all the IPs (`0.0.0.0`) on a predefined port (e.g. `80`).