Run a Server Manually - Uvicorn

    The current page still doesn’t have a translation for this language.

    But you can help translating it: Contributing.

    The main thing you need to run a FastAPI application in a remote server machine is an ASGI server program like Uvicorn.

    There are 3 main alternatives:

    • : a high performance ASGI server.
    • Hypercorn: an ASGI server compatible with HTTP/2 and Trio among other features.
    • : the ASGI server built for Django Channels.

    There’s a small detail about names to have in mind. 💡

    The word “server“ is commonly used to refer to both the remote/cloud computer (the physical or virtual machine) and also the program that is running on that machine (e.g. Uvicorn).

    Just have that in mind when you read “server” in general, it could refer to one of those two things.

    When referring to the remote machine, it’s common to call it server, but also machine, VM (virtual machine), node. Those all refer to some type of remote machine, normally running Linux, where you run programs.

    You can install an ASGI compatible server with:

    Hypercorn

    • Uvicorn, a lightning-fast ASGI server, built on uvloop and httptools.

    By adding the , Uvicorn will install and use some recommended extra dependencies.

    That including uvloop, the high-performance drop-in replacement for asyncio, that provides the big concurrency performance boost.

    Run a Server Manually - Uvicorn - 图2

    …or any other ASGI server.

    You can then run your application the same way you have done in the tutorials, but without the option, e.g.:

    UvicornHypercorn

    Run a Server Manually - Uvicorn - 图4

    Warning

    Remember to remove the --reload option if you were using it.

    The --reload option consumes much more resources, is more unstable, etc.

    Starlette and FastAPI are based on , which makes them compatible with both Python’s standard library asyncio and .

    Nevertheless, Uvicorn is currently only compatible with asyncio, and it normally uses uvloop, the high-performance drop-in replacement for .

    But if you want to directly use Trio, then you can use Hypercorn as it supports it. ✨

    First you need to install Hypercorn with Trio support:

    Run with Trio

    Then you can pass the command line option --worker-class with the value trio:

    Run a Server Manually - Uvicorn - 图6

    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. 🎉

    These examples run the server program (e.g Uvicorn), starting a single process, listening on all the IPs () on a predefined port (e.g. 80).

    This is the basic idea. But you will probably want to take care of some additional things, like:

    • Security - HTTPS
    • Running on startup
    • Restarts
    • Replication (the number of processes running)
    • Memory