mirror of https://github.com/tiangolo/fastapi.git
📝 Add docs for using Trio with Hypercorn (#4014)
This commit is contained in:
parent
c15f042318
commit
d9fa2311b3
|
|
@ -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.
|
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
|
### Other forms of asynchronous code
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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**.
|
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
|
## 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`).
|
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`).
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue