{"title":"如何使用 ASGI 来部署","version":"5.2","locale":"zh-hans","docname":"howto/deployment/asgi/index","url":"/zh-hans/5.2/howto/deployment/asgi/","canonical":"https://djangodocs.dev/zh-hans/5.2/howto/deployment/asgi/","summary":"同 WSGI 一样，Django 也支持使用 ASGI 来部署，它是为了支持异步网络服务器和应用而新出现的 Python 标准。 Django 的管理命令 startproject 生成了一个默认的 ASGI 配置，你可以按照自己项目的需要去调整这个配置，任何兼容 ASGI 的应用程序服务器都可以直接使用。 Django…","html":"<h1>如何使用 ASGI 来部署<a class=\"heading-anchor\" href=\"#how-to-deploy-with-asgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>同 WSGI 一样，Django 也支持使用 <a class=\"reference external\" href=\"https://asgi.readthedocs.io/en/latest/\">ASGI</a> 来部署，它是为了支持异步网络服务器和应用而新出现的 Python 标准。</p>\n<p>Django 的管理命令 <a class=\"reference internal\" href=\"/zh-hans/5.2/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a> 生成了一个默认的 ASGI 配置，你可以按照自己项目的需要去调整这个配置，任何兼容 ASGI 的应用程序服务器都可以直接使用。</p>\n<p>Django 提供了下面这些 ASGI 服务的入门文档：</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/5.2/howto/deployment/asgi/daphne/\">如何使用 Daphne 托管 Django</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/5.2/howto/deployment/asgi/hypercorn/\">如何使用 Hypercorn 托管 Django。</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/zh-hans/5.2/howto/deployment/asgi/uvicorn/\">如何使用 Uvicorn 托管 Django</a></li>\n</ul>\n</div>\n<section id=\"the-application-object\">\n<h2><code class=\"docutils literal notranslate\">application</code> 对象<a class=\"heading-anchor\" href=\"#the-application-object\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>与WSGI类似，ASGI需要你提供 <code class=\"docutils literal notranslate\">application</code> 可以让应用程序服务器用它与你的代码交互。可调用的应用程序 <code class=\"docutils literal notranslate\">application</code> 一般以一个位于 Python 模块中，名为应用程序 <code class=\"docutils literal notranslate\">application</code> 的对象的形式提供，且对服务器可见。</p>\n<p><a class=\"reference internal\" href=\"/zh-hans/5.2/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a> 命令创建了文件 <code class=\"file docutils literal notranslate\">&lt;project_name&gt;/asgi.py</code>，其中包含了 <code class=\"docutils literal notranslate\">application</code> callable应用程序。</p>\n<p>它不会被开发服务器使用 (<code class=\"docutils literal notranslate\">runserver</code>) ，但是可以在开发环境或生产环境中用任意一种ASGI服务器来使用。</p>\n<p>ASGI服务器通常会以字符串形式获取可调用应用程序的路径；对于大多数Django项目来说，应用程序形如 <code class=\"docutils literal notranslate\">myproject.asgi:application</code> 。</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>Django默认的ASGI处理器会将你所有的代码放在一个同步的线程内运行，但你如果选择运行自己的异步处理器，你必须确保它是异步安全的。</p>\n<p>不要在异步代码中调用阻塞的同步函数或库。Django会阻止你这样异步不安全地使用Django的内容，但是对第三方应用程序或者Python库不一定会这样。</p>\n</aside>\n</section>\n<section id=\"configuring-the-settings-module\">\n<h2>配置 settings 模块<a class=\"heading-anchor\" href=\"#configuring-the-settings-module\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>当 ASGI 服务器加载应用程序时，Django 需要导入配置模块——完整定义应用程序的地方。</p>\n<p>Django 利用 <span class=\"target\" id=\"index-2\"></span><a class=\"reference internal\" href=\"/zh-hans/5.2/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\">DJANGO_SETTINGS_MODULE</code></a> 环境变量来定位合适的配置模块。它必须包含到配置模块的点式路径。开发环境和生产环境可以配置不同值；这都取决于你是如何组织配置的。</p>\n<p>若未设置该变量，文件 <code class=\"file docutils literal notranslate\">asgi.py</code> 默认将其配置为 <code class=\"docutils literal notranslate\">mysite.settings</code>， <code class=\"docutils literal notranslate\">mysite</code> 即工程名字。</p>\n</section>\n<section id=\"applying-asgi-middleware\">\n<h2>应用 ASGI 中间件<a class=\"heading-anchor\" href=\"#applying-asgi-middleware\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>为了应用ASGI中间件，或者将Django嵌入到其他的ASGI应用程序中，你可以将Django的 <code class=\"docutils literal notranslate\">application</code> 应用程序对象包装到  <code class=\"docutils literal notranslate\">asgi.py</code> 文件中。举个例子：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span> <span class=\"nn\">some_asgi_library</span> <span class=\"kn\">import</span> AmazingMiddleware\n\napplication <span class=\"o\">=</span> AmazingMiddleware<span class=\"p\">(</span>application<span class=\"p\">)</span>\n</code></pre></div>\n</section>","rootId":"how-to-deploy-with-asgi","toc":[{"title":"application 对象","anchor":"the-application-object","children":[]},{"title":"配置 settings 模块","anchor":"configuring-the-settings-module","children":[]},{"title":"应用 ASGI 中间件","anchor":"applying-asgi-middleware","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"如何引导","url":"/zh-hans/5.2/howto/"},{"docname":"howto/deployment/index","title":"如何部署 Django","url":"/zh-hans/5.2/howto/deployment/"}],"prev":{"docname":"howto/deployment/wsgi/apache-auth","title":"如何从 Apache 对 Django 的用户数据库进行认证","url":"/zh-hans/5.2/howto/deployment/wsgi/apache-auth/"},"next":{"docname":"howto/deployment/asgi/daphne","title":"如何使用 Daphne 托管 Django","url":"/zh-hans/5.2/howto/deployment/asgi/daphne/"},"formats":{"html":"/zh-hans/5.2/howto/deployment/asgi/","markdown":"/zh-hans/5.2/howto/deployment/asgi.md","json":"/zh-hans/5.2/howto/deployment/asgi.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/howto/deployment/asgi/index.txt","official":"https://docs.djangoproject.com/zh-hans/5.2/howto/deployment/asgi/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}