{"title":"ASGI とともにデプロイするには","version":"5.0","locale":"ja","docname":"howto/deployment/asgi/index","url":"/ja/5.0/howto/deployment/asgi/","canonical":"https://djangodocs.dev/ja/5.0/howto/deployment/asgi/","summary":"WSGI だけでなく、Django は非同期ウェブサーバやアプリケーションのための新しい Python 標準である ASGI でのデプロイもサポートしています。","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 は非同期ウェブサーバやアプリケーションのための新しい Python 標準である <a class=\"reference external\" href=\"https://asgi.readthedocs.io/en/latest/\">ASGI</a> でのデプロイもサポートしています。</p>\n<p>Django の <a class=\"reference internal\" href=\"/ja/5.0/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a> 管理用コマンドは、デフォルトの ASGI 設定をセットアップします。必要に応じて、あなたのプロジェクトと ASGI 準拠の Web サーバに合わせて微調整することができます。</p>\n<p>Django には以下の ASGI サーバのために、手引きとなるドキュメントが用意されています。</p>\n<div class=\"toctree-wrapper compound\">\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/ja/5.0/howto/deployment/asgi/daphne/\">Django を Daphne とともに使う</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/ja/5.0/howto/deployment/asgi/hypercorn/\">Django を Hypercorn とともに使う</a></li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"/ja/5.0/howto/deployment/asgi/uvicorn/\">Django を Uvicorn とともに使う</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> という呼び出し可能オブジェクトを提供しており、アプリケーション サーバはこれを使用してあなたのコードと通信します。これは一般的に、サーバにアクセスできる Python モジュールの中で <code class=\"docutils literal notranslate\">application</code> という名前のオブジェクトとして提供されています。</p>\n<p><a class=\"reference internal\" href=\"/ja/5.0/ref/django-admin/#django-admin-startproject\"><code class=\"xref std std-djadmin docutils literal notranslate\">startproject</code></a> コマンドは、この <code class=\"docutils literal notranslate\">application</code> 呼び出し可能オブジェクトを含む <code class=\"file docutils literal notranslate\">&lt;project_name&gt;/asgi.py</code> ファイルを生成します。</p>\n<p>開発サーバー (<code class=\"docutils literal notranslate\">runserver</code>) では使用されませんが、開発環境と本番環境のどちらでも、任意の ASGI サーバーからも使用できます。</p>\n<p>ASGI サーバーは通常、呼び出し可能オブジェクト application へのパスを文字列として取ります。たとえば、ほとんどの Django プロジェクトでは、<code class=\"docutils literal notranslate\">myproject.asgi:application</code> のような文字列になります。</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">警告</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 は settings モジュールをインポートする必要があります――アプリケーション全体がここで定義されます。</p>\n<p>Django は適切な Settings モジュールを見つけるために、 <span class=\"target\" id=\"index-2\"></span><a class=\"reference internal\" href=\"/ja/5.0/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 ミドルウェアを適用したり、他の ASGI アプリケーションに Django を組み込むには、 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":"「How-to」ガイド","url":"/ja/5.0/howto/"},{"docname":"howto/deployment/index","title":"Djangoをデプロイするには","url":"/ja/5.0/howto/deployment/"}],"prev":{"docname":"howto/deployment/wsgi/apache-auth","title":"Django のユーザーデータベースに対する Apache からの認証","url":"/ja/5.0/howto/deployment/wsgi/apache-auth/"},"next":{"docname":"howto/deployment/asgi/daphne","title":"Django を Daphne とともに使う","url":"/ja/5.0/howto/deployment/asgi/daphne/"},"formats":{"html":"/ja/5.0/howto/deployment/asgi/","markdown":"/ja/5.0/howto/deployment/asgi.md","json":"/ja/5.0/howto/deployment/asgi.json"},"source":"https://github.com/django/django/blob/stable/5.0.x/docs/howto/deployment/asgi/index.txt","official":"https://docs.djangoproject.com/ja/5.0/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","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}