如何使用 Daphne 托管 Django

Daphne is a pure-Python ASGI server for UNIX, maintained by members of the Django project. It acts as the reference server for ASGI.

安装 Daphne

You can install Daphne with pip:

python -m pip install daphne

在 Daphne 中运行 Django

一旦 Daphne 安装完毕,你就可以使用 daphne 命令了,它将用来启动 Daphne 服务进程。在最简单的情形下,Daphne 加上包含一个 ASGI 应用模块的位置和应用的名称(以冒号分隔)。

For a typical Django project, invoking Daphne would look like:

daphne myproject.asgi:application

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

Integration with runserver

Daphne provides a runserver command to serve your site under ASGI during development.

This can be enabled by adding daphne to the start of your INSTALLED_APPS and adding an ASGI_APPLICATION setting pointing to your ASGI application object:

INSTALLED_APPS = [
    "daphne",
    ...,
]

ASGI_APPLICATION = "myproject.asgi.application"