---
title: "如何使用 Uvicorn 托管 Django"
version: 4.1
locale: zh-hans
source: https://docs.djangoproject.com/zh-hans/4.1/howto/deployment/asgi/uvicorn/
canonical: https://djangodocs.dev/zh-hans/4.1/howto/deployment/asgi/uvicorn/
---
# 如何使用 Uvicorn 托管 Django

[Uvicorn](https://www.uvicorn.org/) 是一个基于 `uvloop` 和 `httptools` 的加强运行速度的ASGI服务器。

## 安装Uvicorn

你可以通过 `pip` 来安装Uvicorn

```bash
python -m pip install uvicorn
```

## 在 Uvicorn 中运行 Django

一旦 Uvicorn 安装完毕，你就可用 `uvicorn` 命令来运行ASGI应用了。Uvicorn 运行需要包含一个 ASGI 应用程序模块的位置和应用程序的名称（以冒号分隔）。

对于一个典型的 Django 项目，可以像下面这样来启动 Uvicorn

```bash
python -m uvicorn myproject.asgi:application
```

它将开启一个进程，监听 `127.0.0.1:8000`。这需要你的项目位于 Python path 上。为了确保这点，你应该在与 `manage.py` 文件相同的路径中运行这个命令。

In development mode, you can add `--reload` to cause the server to reload any
time a file is changed on disk.

有关更多的高级用法，请参阅 [Uvicorn documentation 1](https://www.uvicorn.org/).

## Deploying Django using Uvicorn and Gunicorn

[Gunicorn](https://gunicorn.org/) is a robust web server that implements process monitoring and automatic
restarts. This can be useful when running Uvicorn in a production environment.

To install Uvicorn and Gunicorn, use the following:

```bash
python -m pip install uvicorn gunicorn
```

Then start Gunicorn using the Uvicorn worker class like this:

```bash
python -m gunicorn myproject.asgi:application -k uvicorn.workers.UvicornWorker
```
