바뀜
편집 요약 없음
Channels. [https://channels.readthedocs.io/en/stable/ 공식링크]를 참고하였다. 사용하는 장고 버전에 따라 진행방식에 차이가 있을 수 있으니 유의하자.
Channels. [https://channels.readthedocs.io/en/stable/ 공식링크]를 참고하였다. 사용하는 장고 버전에 따라 진행방식에 차이가 있을 수 있으니 유의하자.
장고는 기본적으로 동기식으로, WebSockets, chat protocols, IoT protocols 등을 위해선 비동기식 처리가 필요하다. 이를 위해 Django Channels가 마련되어 있다.
=== 버전별 호환 ===
{| class="wikitable"
!channel 버전
!django 버전
!비고
|-
|3
|2.2 이상
|
|-
|4
|
|
|}장고는 기본적으로 동기식으로, WebSockets, chat protocols, IoT protocols 등을 위해선 비동기식 처리가 필요하다. 이를 위해 Django Channels가 마련되어 있다.
ASGI(Async Server Gateway Interface)프로토콜을 토대로 작동하는데, WSGI를 계승하여 이와 잘 호환되도록 설계되어 있다.
ASGI(Async Server Gateway Interface)프로토콜을 토대로 작동하는데, WSGI를 계승하여 이와 잘 호환되도록 설계되어 있다.
|앱 등록
|앱 등록
|settings.py에 추가.
|settings.py에 추가.
channels는 runserver 명령을 제어하여 기존 서버를 대체한다.
channels는 runserver명령을 제어하여 기존 서버를 대체한다.
runserver에 관여하는 다른 앱이 없다면 문제 없이 작동한다.
|INSTALLED_APPS 하위, 가장 처음에 <code>'channels'</code>넣는다.<syntaxhighlight lang="python">
|INSTALLED_APPS 하위, 가장 처음에 <code>'channels'</code>넣는다.<syntaxhighlight lang="python">
INSTALLED_APPS = [
INSTALLED_APPS = [
</syntaxhighlight>
</syntaxhighlight>
|-
|-
|asgi.py 설정
|프로젝트의 asgi.py를 다음과 같이 편집한다.
(장고 2.2라면 asgi.py가 없어 새로 작성해주어야 한다.)
|장고 3.X대라면 아래처럼 덮어주고,<syntaxhighlight lang="python">
import os
from channels.routing import ProtocolTypeRouter
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
# Initialize Django ASGI application early to ensure the AppRegistry
# is populated before importing code that may import ORM models.
django_asgi_app = get_asgi_application()
application = ProtocolTypeRouter({
"http": django_asgi_app,
# Just HTTP for now. (We can add other protocols later.)
})
</syntaxhighlight>장고 2.2대라면 아래처럼 settings.py와 같은 경로에 새로 작성해준다.<syntaxhighlight lang="python">
import os
import django
from channels.http import AsgiHandler
from channels.routing import ProtocolTypeRouter
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
django.setup()
application = ProtocolTypeRouter({
"http": AsgiHandler(),
# Just HTTP for now. (We can add other protocols later.)
})
</syntaxhighlight>|-
|라우팅 설정 작성
|라우팅 설정 작성
|가장 상위의 디렉터리에 routing.py를 다음과 같이 작성한다.
|가장 상위의 디렉터리에 routing.py를 다음과 같이 작성한다.